Skip to content

Commit cdf701c

Browse files
authored
Update house_robber.cpp
1 parent 67fa9f6 commit cdf701c

File tree

1 file changed

+4
-18
lines changed

1 file changed

+4
-18
lines changed

dynamic_programming/house_robber.cpp

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,14 @@ std::uint64_t houseRobber(const std::vector<int> &money, int n) {
3434
if (n == 1) { // if there is only one house
3535
return money[0];
3636
}
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]);
4339
}
4440
int max_value = 0; // contains maximum stolen value at the end
4541
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]);
5243
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);
5845
value1 = value2;
5946
value2 = max_value;
6047
}
@@ -89,7 +76,6 @@ static void test() {
8976
// third, fifth and seventh with total sum money as 19
9077
std::cout << "passed" << std::endl;
9178

92-
// Test 3
9379
// [] return 0
9480
std::vector<int> array3 = {};
9581
std::cout << "Test 3... ";

0 commit comments

Comments
 (0)