Skip to content

Commit 39c3719

Browse files
authored
Update house_robber.cpp
1 parent 50279f7 commit 39c3719

File tree

1 file changed

+10
-22
lines changed

1 file changed

+10
-22
lines changed

dynamic_programming/house_robber.cpp

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
* @file
33
* @brief Implementation of [House Robber Problem] algorithm
44
* @details
5-
* Solution of House robber problem uses a dynamic programming concept that
6-
* works in \f$O(n)\f$ time and works in \f$O(1)\f$ space.
5+
* Solution of House robber problem uses a dynamic programming concept that works in \f$O(n)\f$ time and works in \f$O(1)\f$ space.
76
*/
87

98
#include <cassert> /// for assert
@@ -21,8 +20,7 @@ namespace dynamic_programming {
2120
*/
2221
namespace house_robber {
2322
/**
24-
* @brief The main function implements house robber algorithm using dynamic
25-
* programming
23+
* @brief The main function implements house robber algorithm using dynamic programming
2624
* @param money array containing money in the ith house
2725
* @param n size of array
2826
* @returns maximum amount of money that can be robbed
@@ -34,8 +32,7 @@ std::uint64_t houseRobber(const std::vector<int> &money, int n) {
3432
if (n == 1) { // if there is only one house
3533
return money[0];
3634
}
37-
if (n == 2) { // if there are two houses, one with the maximum amount of
38-
// money will be robbed
35+
if (n == 2) { // if there are two houses, one with the maximum amount of money will be robbed
3936
return std::max(money[0], money[1]);
4037
}
4138
int max_value = 0; // contains maximum stolen value at the end
@@ -61,37 +58,28 @@ static void test() {
6158
// [1, 2, 3, 1] return 4
6259
std::vector<int> array1 = {1, 2, 3, 1};
6360
std::cout << "Test 1... ";
64-
assert(
65-
dynamic_programming::house_robber::houseRobber(array1, array1.size()) ==
66-
4); // here the two non-adjacent houses that are robbed are first and
67-
// third with total sum money as 4
61+
assert(dynamic_programming::house_robber::houseRobber(array1, array1.size()) == 4); // here the two non-adjacent houses that are robbed are first and third with total sum money as 4
6862
std::cout << "passed" << std::endl;
6963

7064
// Test 2
7165
// [6, 7, 1, 3, 8, 2, 4] return 19
7266
std::vector<int> array2 = {6, 7, 1, 3, 8, 2, 4};
7367
std::cout << "Test 2... ";
74-
assert(
75-
dynamic_programming::house_robber::houseRobber(array2, array2.size()) ==
76-
19); // here the four non-adjacent houses that are robbed are first,
77-
// third, fifth and seventh with total sum money as 19
68+
assert(dynamic_programming::house_robber::houseRobber(array2, array2.size()) == 19); // here the four non-adjacent houses that are robbed are first, third, fifth and seventh with total sum money as 19
7869
std::cout << "passed" << std::endl;
79-
70+
71+
// Test 3
8072
// [] return 0
8173
std::vector<int> array3 = {};
8274
std::cout << "Test 3... ";
83-
assert(
84-
dynamic_programming::house_robber::houseRobber(array3, array3.size()) ==
85-
0); // since there is no house no money can be robbed
75+
assert(dynamic_programming::house_robber::houseRobber(array3, array3.size()) == 0); // since there is no house no money can be robbed
8676
std::cout << "passed" << std::endl;
8777

78+
// Test 4
8879
// [2,7,9,3,1] return 12
8980
std::vector<int> array4 = {2, 7, 9, 3, 1};
9081
std::cout << "Test 4... ";
91-
assert(
92-
dynamic_programming::house_robber::houseRobber(array4, array4.size()) ==
93-
12); // here the three non-adjacent houses that are robbed are first,
94-
// third and fifth with total sum money as 12
82+
assert(dynamic_programming::house_robber::houseRobber(array4, array4.size()) == 12); // here the three non-adjacent houses that are robbed are first, third and fifth with total sum money as 12
9583
std::cout << "passed" << std::endl;
9684
}
9785

0 commit comments

Comments
 (0)