File tree Expand file tree Collapse file tree 1 file changed +4
-18
lines changed Expand file tree Collapse file tree 1 file changed +4
-18
lines changed Original file line number Diff line number Diff line change @@ -34,27 +34,14 @@ std::uint64_t houseRobber(const std::vector<int> &money, int n) {
34
34
if (n == 1 ) { // if there is only one house
35
35
return money[0 ];
36
36
}
37
- if (n == 2 ) { // if there are two houses, one with the maximum amount of
38
- // money will be robbed
39
- if (money[0 ] > money[1 ]) {
40
- return money[0 ];
41
- }
42
- return money[1 ];
37
+ if (n == 2 ) { // if there are two houses, one with the maximum amount of money will be robbed
38
+ return std::max (money[0 ],money[1 ]);
43
39
}
44
40
int max_value = 0 ; // contains maximum stolen value at the end
45
41
int value1 = money[0 ];
46
- int value2 = 0 ;
47
- if (money[0 ] > money[1 ]) {
48
- value2 = money[0 ];
49
- } else {
50
- value2 = money[1 ];
51
- }
42
+ int value2 = std::max (money[0 ],money[1 ]);
52
43
for (int i = 2 ; i < n; i++) {
53
- if (money[i] + value1 > value2) {
54
- max_value = (money[i] + value1);
55
- } else {
56
- max_value = (value2);
57
- }
44
+ max_value = std::max (money[i]+value1,value2);
58
45
value1 = value2;
59
46
value2 = max_value;
60
47
}
@@ -89,7 +76,6 @@ static void test() {
89
76
// third, fifth and seventh with total sum money as 19
90
77
std::cout << " passed" << std::endl;
91
78
92
- // Test 3
93
79
// [] return 0
94
80
std::vector<int > array3 = {};
95
81
std::cout << " Test 3... " ;
You can’t perform that action at this time.
0 commit comments