From 6115bc298154dc996c0315e79673fa436d02f6ea Mon Sep 17 00:00:00 2001 From: Swastika Gupta <64654203+Swastyy@users.noreply.github.com> Date: Sat, 10 Jul 2021 00:32:28 +0530 Subject: [PATCH] feat: Add House Robber algorithm (#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 c00823e8 * Update house_robber.cpp * clang-format and clang-tidy fixes for cdf701c2 * Update house_robber.cpp * clang-format and clang-tidy fixes for 39c3719f * Update dynamic_programming/house_robber.cpp Co-authored-by: Abhinn Mishra <49574460+mishraabhinn@users.noreply.github.com> * clang-format and clang-tidy fixes for 126e3f21 * Update house_robber.cpp * Update dynamic_programming/house_robber.cpp Co-authored-by: David Leal * Update dynamic_programming/house_robber.cpp Co-authored-by: David Leal * Update dynamic_programming/house_robber.cpp Co-authored-by: David Leal * Update dynamic_programming/house_robber.cpp Co-authored-by: David Leal * Update house_robber.cpp * clang-format and clang-tidy fixes for 474a5f0b * Update dynamic_programming/house_robber.cpp Co-authored-by: David Leal * Update dynamic_programming/house_robber.cpp Co-authored-by: David Leal * clang-format and clang-tidy fixes for 203cce31 * Update house_robber.cpp * Update house_robber.cpp * Update house_robber.cpp * clang-format and clang-tidy fixes for 6b0bea93 * Apply suggestions from code review * Apply suggestions from code review * clang-format and clang-tidy fixes for 913baf88 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 --- DIRECTORY.md | 1 + dynamic_programming/house_robber.cpp | 114 +++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 dynamic_programming/house_robber.cpp diff --git a/DIRECTORY.md b/DIRECTORY.md index b65d77995..ea7ed6779 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -74,6 +74,7 @@ * [Egg Dropping Puzzle](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/egg_dropping_puzzle.cpp) * [Fibonacci Bottom Up](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/fibonacci_bottom_up.cpp) * [Floyd Warshall](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/floyd_warshall.cpp) + * [House Robber](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/house_robber.cpp) * [Kadane](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/kadane.cpp) * [Kadane2](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/kadane2.cpp) * [Longest Common String](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/longest_common_string.cpp) diff --git a/dynamic_programming/house_robber.cpp b/dynamic_programming/house_robber.cpp new file mode 100644 index 000000000..0d238b9c1 --- /dev/null +++ b/dynamic_programming/house_robber.cpp @@ -0,0 +1,114 @@ +/** + * @file + * @brief Implementation of [House Robber + * Problem](https://labuladong.gitbook.io/algo-en/i.-dynamic-programming/houserobber) + * algorithm + * @details + * 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. + * @author [Swastika Gupta](https://github.com/Swastyy) + */ + +#include /// for assert +#include /// for std::max +#include /// for io operations +#include /// for std::vector + +/** + * @namespace dynamic_programming + * @brief Dynamic Programming algorithms + */ +namespace dynamic_programming { +/** + * @namespace house_robber + * @brief Functions for the [House + * Robber](https://labuladong.gitbook.io/algo-en/i.-dynamic-programming/houserobber) + * algorithm + */ +namespace house_robber { +/** + * @brief The main function that implements the House Robber algorithm using + * dynamic programming + * @param money array containing money in the ith house + * @param n size of array + * @returns maximum amount of money that can be robbed + */ +std::uint32_t houseRobber(const std::vector &money, + const uint32_t &n) { + if (n == 0) { // if there is no house + return 0; + } + if (n == 1) { // if there is only one house + return money[0]; + } + if (n == 2) { // if there are two houses, one with the maximum amount of + // money will be robbed + return std::max(money[0], money[1]); + } + uint32_t max_value = 0; // contains maximum stolen value at the end + uint32_t value1 = money[0]; + uint32_t value2 = std::max(money[0], money[1]); + for (uint32_t i = 2; i < n; i++) { + max_value = std::max(money[i] + value1, value2); + value1 = value2; + value2 = max_value; + } + + return max_value; +} +} // namespace house_robber +} // namespace dynamic_programming + +/** + * @brief Self-test implementations + * @returns void + */ +static void test() { + // Test 1 + // [1, 2, 3, 1] return 4 + std::vector array1 = {1, 2, 3, 1}; + std::cout << "Test 1... "; + 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 + std::cout << "passed" << std::endl; + + // Test 2 + // [6, 7, 1, 3, 8, 2, 4] return 19 + std::vector array2 = {6, 7, 1, 3, 8, 2, 4}; + std::cout << "Test 2... "; + 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 + std::cout << "passed" << std::endl; + + // Test 3 + // [] return 0 + std::vector array3 = {}; + std::cout << "Test 3... "; + assert( + dynamic_programming::house_robber::houseRobber(array3, array3.size()) == + 0); // since there is no house no money can be robbed + std::cout << "passed" << std::endl; + + // Test 4 + // [2,7,9,3,1] return 12 + std::vector array4 = {2, 7, 9, 3, 1}; + std::cout << "Test 4... "; + 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 + std::cout << "passed" << std::endl; +} + +/** + * @brief Main function + * @returns 0 on exit + */ +int main() { + test(); // run self-test implementations + return 0; +}