mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
Update house_robber.cpp
This commit is contained in:
parent
d8db9a8d05
commit
262f7896bf
@ -1,11 +1,8 @@
|
|||||||
/**
|
/**
|
||||||
* @file
|
* @file
|
||||||
* @brief Implementation of [House Robber
|
* @brief Implementation of [House Robber Problem](https://labuladong.gitbook.io/algo-en/i.-dynamic-programming/houserobber) algorithm
|
||||||
* Problem](https://labuladong.gitbook.io/algo-en/i.-dynamic-programming/houserobber)
|
|
||||||
* algorithm
|
|
||||||
* @details
|
* @details
|
||||||
* Solution of House robber problem uses a dynamic programming concept that
|
* 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.
|
||||||
* works in \f$O(n)\f$ time and works in \f$O(1)\f$ space.
|
|
||||||
* @author [Swastika Gupta](https://github.com/Swastyy)
|
* @author [Swastika Gupta](https://github.com/Swastyy)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -20,33 +17,30 @@
|
|||||||
namespace dynamic_programming {
|
namespace dynamic_programming {
|
||||||
/**
|
/**
|
||||||
* @namespace house_robber
|
* @namespace house_robber
|
||||||
* @brief Functions for the [House
|
* @brief Functions for the [House Robber](https://labuladong.gitbook.io/algo-en/i.-dynamic-programming/houserobber)
|
||||||
* Robber](https://labuladong.gitbook.io/algo-en/i.-dynamic-programming/houserobber)
|
|
||||||
* algorithm
|
* algorithm
|
||||||
*/
|
*/
|
||||||
namespace house_robber {
|
namespace house_robber {
|
||||||
/**
|
/**
|
||||||
* @brief The main function implements house robber algorithm using dynamic
|
* @brief The main function implements house robber algorithm using dynamic programming
|
||||||
* programming
|
|
||||||
* @param money array containing money in the ith house
|
* @param money array containing money in the ith house
|
||||||
* @param n size of array
|
* @param n size of array
|
||||||
* @returns maximum amount of money that can be robbed
|
* @returns maximum amount of money that can be robbed
|
||||||
*/
|
*/
|
||||||
std::uint32_t houseRobber(const std::vector<int> &money, const int &n) {
|
std::uint32_t houseRobber(const std::vector<uint32_t> &money, const uint32_t &n) {
|
||||||
if (n == 0) { // if there is no house
|
if (n == 0) { // if there is no house
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if (n == 1) { // if there is only one house
|
if (n == 1) { // if there is only one house
|
||||||
return money[0];
|
return money[0];
|
||||||
}
|
}
|
||||||
if (n == 2) { // if there are two houses, one with the maximum amount of
|
if (n == 2) { // if there are two houses, one with the maximum amount of money will be robbed
|
||||||
// money will be robbed
|
|
||||||
return std::max(money[0], money[1]);
|
return std::max(money[0], money[1]);
|
||||||
}
|
}
|
||||||
int max_value = 0; // contains maximum stolen value at the end
|
uint32_t max_value = 0; // contains maximum stolen value at the end
|
||||||
int value1 = money[0];
|
uint32_t value1 = money[0];
|
||||||
int value2 = std::max(money[0], money[1]);
|
uint32_t value2 = std::max(money[0], money[1]);
|
||||||
for (int i = 2; i < n; i++) {
|
for (uint32_t i = 2; i < n; i++) {
|
||||||
max_value = std::max(money[i] + value1, value2);
|
max_value = std::max(money[i] + value1, value2);
|
||||||
value1 = value2;
|
value1 = value2;
|
||||||
value2 = max_value;
|
value2 = max_value;
|
||||||
@ -66,39 +60,28 @@ static void test() {
|
|||||||
// [1, 2, 3, 1] return 4
|
// [1, 2, 3, 1] return 4
|
||||||
std::vector<int> array1 = {1, 2, 3, 1};
|
std::vector<int> array1 = {1, 2, 3, 1};
|
||||||
std::cout << "Test 1... ";
|
std::cout << "Test 1... ";
|
||||||
assert(
|
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
|
||||||
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
|
|
||||||
std::cout << "passed" << std::endl;
|
std::cout << "passed" << std::endl;
|
||||||
|
|
||||||
// Test 2
|
// Test 2
|
||||||
// [6, 7, 1, 3, 8, 2, 4] return 19
|
// [6, 7, 1, 3, 8, 2, 4] return 19
|
||||||
std::vector<int> array2 = {6, 7, 1, 3, 8, 2, 4};
|
std::vector<int> array2 = {6, 7, 1, 3, 8, 2, 4};
|
||||||
std::cout << "Test 2... ";
|
std::cout << "Test 2... ";
|
||||||
assert(
|
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
|
||||||
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
|
|
||||||
std::cout << "passed" << std::endl;
|
std::cout << "passed" << std::endl;
|
||||||
|
|
||||||
// Test 3
|
// Test 3
|
||||||
// [] return 0
|
// [] return 0
|
||||||
std::vector<int> array3 = {};
|
std::vector<int> array3 = {};
|
||||||
std::cout << "Test 3... ";
|
std::cout << "Test 3... ";
|
||||||
assert(
|
assert(dynamic_programming::house_robber::houseRobber(array3, array3.size()) == 0); // since there is no house no money can be robbed
|
||||||
dynamic_programming::house_robber::houseRobber(array3, array3.size()) ==
|
|
||||||
0); // since there is no house no money can be robbed
|
|
||||||
std::cout << "passed" << std::endl;
|
std::cout << "passed" << std::endl;
|
||||||
|
|
||||||
// Test 4
|
// Test 4
|
||||||
// [2,7,9,3,1] return 12
|
// [2,7,9,3,1] return 12
|
||||||
std::vector<int> array4 = {2, 7, 9, 3, 1};
|
std::vector<int> array4 = {2, 7, 9, 3, 1};
|
||||||
std::cout << "Test 4... ";
|
std::cout << "Test 4... ";
|
||||||
assert(
|
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
|
||||||
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
|
|
||||||
std::cout << "passed" << std::endl;
|
std::cout << "passed" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user