TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
Loading...
Searching...
No Matches
house_robber.cpp
Go to the documentation of this file.
1
11
12
#include <cassert>
13
#include <climits>
14
#include <cstdint>
15
#include <iostream>
16
#include <vector>
21
namespace
dynamic_programming
{
28
namespace
house_robber
{
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
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
111
int
main
() {
112
test
();
// run self-test implementations
113
return
0;
114
}
test
void test()
Definition
caesar_cipher.cpp:100
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
dynamic_programming::house_robber::houseRobber
std::uint32_t houseRobber(const std::vector< uint32_t > &money, const uint32_t &n)
The main function that implements the House Robber algorithm using dynamic programming.
Definition
house_robber.cpp:36
dynamic_programming
Dynamic Programming algorithms.
house_robber
Functions for the House Robber algorithm.
dynamic_programming
house_robber.cpp
Generated by
1.18.0