|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @brief Implementation of [House Robber Problem] algorithm |
| 4 | + * @details |
| 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. |
| 6 | + */ |
| 7 | + |
| 8 | +#include <cassert> /// for assert |
| 9 | +#include <iostream> /// for io operations |
| 10 | +#include <vector> /// for std::vector |
| 11 | + |
| 12 | +/** |
| 13 | + * @namespace dynamic_programming |
| 14 | + * @brief House robber algorithm |
| 15 | + */ |
| 16 | +namespace dynamic_programming { |
| 17 | +/** |
| 18 | + * @namespace house_robber |
| 19 | + * @brief Functions for House Robber algorithm |
| 20 | + */ |
| 21 | +namespace house_robber { |
| 22 | +/** |
| 23 | + * @brief The main function implements house robber algorithm using dynamic programming |
| 24 | + * @param money array containing money in the ith house |
| 25 | + * @param n size of array |
| 26 | + * @returns maximum amount of money that can be robbed |
| 27 | + */ |
| 28 | +std::uint64_t houseRobber(const std::vector<int> &money, int n) { |
| 29 | + |
| 30 | + if (n == 0) { // if there is no house |
| 31 | + return 0; |
| 32 | + } |
| 33 | + if (n == 1) { // if there is only one house |
| 34 | + return money[0]; |
| 35 | + } |
| 36 | + if (n == 2) { // if there are two houses, one with the maximum amount of money will be robbed |
| 37 | + if (money[0] > money[1]) { |
| 38 | + return money[0]; |
| 39 | + } |
| 40 | + return money[1]; |
| 41 | + } |
| 42 | + int max_value; // contains maximum stolen value at the end |
| 43 | + int value1 = money[0]; |
| 44 | + int value2; |
| 45 | + if (money[0] > money[1]) { |
| 46 | + value2 = money[0]; |
| 47 | + } |
| 48 | + else{ |
| 49 | + value2 = money[1]; |
| 50 | + } |
| 51 | + for (int i=2; i<n; i++) { |
| 52 | + if (money[i]+value1 > value2) { |
| 53 | + max_value = (money[i]+value1); |
| 54 | + } |
| 55 | + else{ |
| 56 | + max_value = (value2); |
| 57 | + } |
| 58 | + value1 = value2; |
| 59 | + value2 = max_value; |
| 60 | + } |
| 61 | + |
| 62 | + return max_value; |
| 63 | +} |
| 64 | +} // namespace house_robber |
| 65 | +} // namespace dynamic_programming |
| 66 | + |
| 67 | +/** |
| 68 | + * @brief Test implementations |
| 69 | + * @returns void |
| 70 | + */ |
| 71 | +static void test() { |
| 72 | + // [1, 2, 3, 1] return 4 |
| 73 | + std::vector<int> array1 = {1, 2, 3, 1}; |
| 74 | + std::cout << "Test 1... "; |
| 75 | + 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 |
| 76 | + std::cout << "passed" << std::endl; |
| 77 | + |
| 78 | + // [6, 7, 1, 3, 8, 2, 4] return 19 |
| 79 | + std::vector<int> array2 = {6, 7, 1, 3, 8, 2, 4}; |
| 80 | + std::cout << "Test 2... "; |
| 81 | + 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 |
| 82 | + std::cout << "passed" << std::endl; |
| 83 | + |
| 84 | + // [] return 0 |
| 85 | + std::vector<int> array3 = {}; |
| 86 | + std::cout << "Test 3... "; |
| 87 | + assert(dynamic_programming::house_robber::houseRobber(array3,array3.size())==0); // since there is no house no money can be robbed |
| 88 | + std::cout << "passed" << std::endl; |
| 89 | + |
| 90 | + // [2,7,9,3,1] return 12 |
| 91 | + std::vector<int> array4 = {2,7,9,3,1}; |
| 92 | + std::cout << "Test 4... "; |
| 93 | + 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 |
| 94 | + std::cout << "passed" << std::endl; |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * @brief Main function |
| 99 | + * @returns 0 on exit |
| 100 | + */ |
| 101 | +int main() { |
| 102 | + test(); // execute the test |
| 103 | + return 0; |
| 104 | +} |
0 commit comments