From 39c3719f3ab5dff47a1e977a2a6b39f8e98521bd Mon Sep 17 00:00:00 2001 From: Swastika Gupta <64654203+Swastyy@users.noreply.github.com> Date: Sun, 4 Jul 2021 08:42:34 +0530 Subject: [PATCH] Update house_robber.cpp --- dynamic_programming/house_robber.cpp | 32 +++++++++------------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/dynamic_programming/house_robber.cpp b/dynamic_programming/house_robber.cpp index 92af4d2ce..7e835723e 100644 --- a/dynamic_programming/house_robber.cpp +++ b/dynamic_programming/house_robber.cpp @@ -2,8 +2,7 @@ * @file * @brief Implementation of [House Robber Problem] 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. + * 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. */ #include /// for assert @@ -21,8 +20,7 @@ namespace dynamic_programming { */ namespace house_robber { /** - * @brief The main function implements house robber algorithm using dynamic - * programming + * @brief The main function implements 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 @@ -34,8 +32,7 @@ std::uint64_t houseRobber(const std::vector &money, int n) { 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 + 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]); } int max_value = 0; // contains maximum stolen value at the end @@ -61,37 +58,28 @@ static void test() { // [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 + 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 + 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 + 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 + 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; }