Skip to content

Commit 6115bc2

Browse files
Swastyygithub-actionsmishraabhinnPanquesito7
authored
feat: Add House Robber algorithm (TheAlgorithms#1524)
* Create house_robber.cpp * updating DIRECTORY.md * Update dynamic_programming/house_robber.cpp Co-authored-by: Abhinn Mishra <49574460+mishraabhinn@users.noreply.github.com> * Update dynamic_programming/house_robber.cpp Co-authored-by: Abhinn Mishra <49574460+mishraabhinn@users.noreply.github.com> * Update dynamic_programming/house_robber.cpp Co-authored-by: Abhinn Mishra <49574460+mishraabhinn@users.noreply.github.com> * clang-format and clang-tidy fixes for c00823e * Update house_robber.cpp * clang-format and clang-tidy fixes for cdf701c * Update house_robber.cpp * clang-format and clang-tidy fixes for 39c3719 * Update dynamic_programming/house_robber.cpp Co-authored-by: Abhinn Mishra <49574460+mishraabhinn@users.noreply.github.com> * clang-format and clang-tidy fixes for 126e3f2 * Update house_robber.cpp * Update dynamic_programming/house_robber.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Update dynamic_programming/house_robber.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Update dynamic_programming/house_robber.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Update dynamic_programming/house_robber.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Update house_robber.cpp * clang-format and clang-tidy fixes for 474a5f0 * Update dynamic_programming/house_robber.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Update dynamic_programming/house_robber.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * clang-format and clang-tidy fixes for 203cce3 * Update house_robber.cpp * Update house_robber.cpp * Update house_robber.cpp * clang-format and clang-tidy fixes for 6b0bea9 * Apply suggestions from code review * Apply suggestions from code review * clang-format and clang-tidy fixes for 913baf8 Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Abhinn Mishra <49574460+mishraabhinn@users.noreply.github.com> Co-authored-by: David Leal <halfpacho@gmail.com>
1 parent 1ab5e4e commit 6115bc2

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
* [Egg Dropping Puzzle](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/egg_dropping_puzzle.cpp)
7575
* [Fibonacci Bottom Up](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/fibonacci_bottom_up.cpp)
7676
* [Floyd Warshall](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/floyd_warshall.cpp)
77+
* [House Robber](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/house_robber.cpp)
7778
* [Kadane](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/kadane.cpp)
7879
* [Kadane2](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/kadane2.cpp)
7980
* [Longest Common String](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/longest_common_string.cpp)

dynamic_programming/house_robber.cpp

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/**
2+
* @file
3+
* @brief Implementation of [House Robber
4+
* Problem](https://labuladong.gitbook.io/algo-en/i.-dynamic-programming/houserobber)
5+
* algorithm
6+
* @details
7+
* Solution of House robber problem uses a dynamic programming concept that
8+
* works in \f$O(n)\f$ time and works in \f$O(1)\f$ space.
9+
* @author [Swastika Gupta](https://github.com/Swastyy)
10+
*/
11+
12+
#include <cassert> /// for assert
13+
#include <climits> /// for std::max
14+
#include <iostream> /// for io operations
15+
#include <vector> /// for std::vector
16+
17+
/**
18+
* @namespace dynamic_programming
19+
* @brief Dynamic Programming algorithms
20+
*/
21+
namespace dynamic_programming {
22+
/**
23+
* @namespace house_robber
24+
* @brief Functions for the [House
25+
* Robber](https://labuladong.gitbook.io/algo-en/i.-dynamic-programming/houserobber)
26+
* algorithm
27+
*/
28+
namespace house_robber {
29+
/**
30+
* @brief The main function that implements the House Robber algorithm using
31+
* dynamic programming
32+
* @param money array containing money in the ith house
33+
* @param n size of array
34+
* @returns maximum amount of money that can be robbed
35+
*/
36+
std::uint32_t houseRobber(const std::vector<uint32_t> &money,
37+
const uint32_t &n) {
38+
if (n == 0) { // if there is no house
39+
return 0;
40+
}
41+
if (n == 1) { // if there is only one house
42+
return money[0];
43+
}
44+
if (n == 2) { // if there are two houses, one with the maximum amount of
45+
// money will be robbed
46+
return std::max(money[0], money[1]);
47+
}
48+
uint32_t max_value = 0; // contains maximum stolen value at the end
49+
uint32_t value1 = money[0];
50+
uint32_t value2 = std::max(money[0], money[1]);
51+
for (uint32_t i = 2; i < n; i++) {
52+
max_value = std::max(money[i] + value1, value2);
53+
value1 = value2;
54+
value2 = max_value;
55+
}
56+
57+
return max_value;
58+
}
59+
} // namespace house_robber
60+
} // namespace dynamic_programming
61+
62+
/**
63+
* @brief Self-test implementations
64+
* @returns void
65+
*/
66+
static void test() {
67+
// Test 1
68+
// [1, 2, 3, 1] return 4
69+
std::vector<uint32_t> array1 = {1, 2, 3, 1};
70+
std::cout << "Test 1... ";
71+
assert(
72+
dynamic_programming::house_robber::houseRobber(array1, array1.size()) ==
73+
4); // here the two non-adjacent houses that are robbed are first and
74+
// third with total sum money as 4
75+
std::cout << "passed" << std::endl;
76+
77+
// Test 2
78+
// [6, 7, 1, 3, 8, 2, 4] return 19
79+
std::vector<uint32_t> array2 = {6, 7, 1, 3, 8, 2, 4};
80+
std::cout << "Test 2... ";
81+
assert(
82+
dynamic_programming::house_robber::houseRobber(array2, array2.size()) ==
83+
19); // here the four non-adjacent houses that are robbed are first,
84+
// third, fifth and seventh with total sum money as 19
85+
std::cout << "passed" << std::endl;
86+
87+
// Test 3
88+
// [] return 0
89+
std::vector<uint32_t> array3 = {};
90+
std::cout << "Test 3... ";
91+
assert(
92+
dynamic_programming::house_robber::houseRobber(array3, array3.size()) ==
93+
0); // since there is no house no money can be robbed
94+
std::cout << "passed" << std::endl;
95+
96+
// Test 4
97+
// [2,7,9,3,1] return 12
98+
std::vector<uint32_t> array4 = {2, 7, 9, 3, 1};
99+
std::cout << "Test 4... ";
100+
assert(
101+
dynamic_programming::house_robber::houseRobber(array4, array4.size()) ==
102+
12); // here the three non-adjacent houses that are robbed are first,
103+
// third and fifth with total sum money as 12
104+
std::cout << "passed" << std::endl;
105+
}
106+
107+
/**
108+
* @brief Main function
109+
* @returns 0 on exit
110+
*/
111+
int main() {
112+
test(); // run self-test implementations
113+
return 0;
114+
}

0 commit comments

Comments
 (0)