From 917c05d83e4ae81ae699c9357e03165f1e522f0d Mon Sep 17 00:00:00 2001 From: Debashis Nandi Date: Wed, 30 Jul 2025 22:10:49 +0530 Subject: [PATCH 01/10] feature: dp pattern 1 fibonacci, tribonacci added --- .../0001_FibonacciNumber.h | 23 +++++++++++++ .../0002_TribonacciNumber.h | 23 +++++++++++++ .../0001_FibonacciNumber.cc | 28 +++++++++++++++ .../0002_TribonacciNumber.cc | 33 ++++++++++++++++++ source/0005_DynamicProgramming/CMakeLists.txt | 9 +++++ .../0001_FibonacciNumberTest.cc | 34 +++++++++++++++++++ .../0002_TribonacciNumberTest.cc | 34 +++++++++++++++++++ test/0005_DynamicProgramming/CMakeLists.txt | 34 +++++++++++++++++++ 8 files changed, 218 insertions(+) create mode 100644 include/0005_DynamicProgramming/0001_FibonacciNumber.h create mode 100644 include/0005_DynamicProgramming/0002_TribonacciNumber.h create mode 100644 source/0005_DynamicProgramming/0001_FibonacciNumber.cc create mode 100644 source/0005_DynamicProgramming/0002_TribonacciNumber.cc create mode 100644 test/0005_DynamicProgramming/0001_FibonacciNumberTest.cc create mode 100644 test/0005_DynamicProgramming/0002_TribonacciNumberTest.cc diff --git a/include/0005_DynamicProgramming/0001_FibonacciNumber.h b/include/0005_DynamicProgramming/0001_FibonacciNumber.h new file mode 100644 index 0000000..f8ebc38 --- /dev/null +++ b/include/0005_DynamicProgramming/0001_FibonacciNumber.h @@ -0,0 +1,23 @@ +#pragma once +#include +using namespace std; + +/* +Pattern 1 +Linear Recurrence + +Description +Print the n'th Fibonacci number. + +*/ + +namespace FibonacciNumber +{ + class DynamicProgramming + { + private: + public: + int RecursiveNthFibonacci(int n); + int DpNthFibonacci(int n); + }; +} \ No newline at end of file diff --git a/include/0005_DynamicProgramming/0002_TribonacciNumber.h b/include/0005_DynamicProgramming/0002_TribonacciNumber.h new file mode 100644 index 0000000..9086db6 --- /dev/null +++ b/include/0005_DynamicProgramming/0002_TribonacciNumber.h @@ -0,0 +1,23 @@ +#pragma once +#include +using namespace std; + +/* +Pattern 1 +Linear Recurrence + +Description +Print the n'th Tribonacci number. + +*/ + +namespace TribonacciNumber +{ + class DynamicProgramming + { + private: + public: + int RecursiveNthTribonacci(int n); + int DpNthTribonacci(int n); + }; +} \ No newline at end of file diff --git a/source/0005_DynamicProgramming/0001_FibonacciNumber.cc b/source/0005_DynamicProgramming/0001_FibonacciNumber.cc new file mode 100644 index 0000000..0c350db --- /dev/null +++ b/source/0005_DynamicProgramming/0001_FibonacciNumber.cc @@ -0,0 +1,28 @@ +#include "../../include/0005_DynamicProgramming/0001_FibonacciNumber.h" + +namespace FibonacciNumber +{ + int DynamicProgramming::RecursiveNthFibonacci(int n) + { + if (n <= 1) + { + return n; + } + + return this->RecursiveNthFibonacci(n - 1) + this->RecursiveNthFibonacci(n - 2); + } + + int DynamicProgramming::DpNthFibonacci(int n) + { + vector dp(n + 1, 0); + dp[0] = 0; + dp[1] = 1; + + for (int i = 2; i <= n; i++) + { + dp[i] = dp[i - 1] + dp[i - 2]; + } + + return dp[n]; + } +} diff --git a/source/0005_DynamicProgramming/0002_TribonacciNumber.cc b/source/0005_DynamicProgramming/0002_TribonacciNumber.cc new file mode 100644 index 0000000..af526e6 --- /dev/null +++ b/source/0005_DynamicProgramming/0002_TribonacciNumber.cc @@ -0,0 +1,33 @@ +#include "../../include/0005_DynamicProgramming/0002_TribonacciNumber.h" + +namespace TribonacciNumber +{ + int DynamicProgramming::RecursiveNthTribonacci(int n) + { + if (n == 0 || n == 1 || n == 2) + { + return 0; + } + + if (n == 3) + { + return 1; + } + + return this->RecursiveNthTribonacci(n - 1) + this->RecursiveNthTribonacci(n - 2) + this->RecursiveNthTribonacci(n - 3); + } + + int DynamicProgramming::DpNthTribonacci(int n) + { + vector dp(n, 0); + dp[0] = dp[1] = 0; + dp[2] = 1; + + for (int i = 3; i < n; i++) + { + dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3]; + } + + return dp[n - 1]; + } +} diff --git a/source/0005_DynamicProgramming/CMakeLists.txt b/source/0005_DynamicProgramming/CMakeLists.txt index e69de29..c0a26ef 100644 --- a/source/0005_DynamicProgramming/CMakeLists.txt +++ b/source/0005_DynamicProgramming/CMakeLists.txt @@ -0,0 +1,9 @@ +# Specify the source files +set(0005DYNAMICPROGRAMMING_SOURCES + 0001_FibonacciNumber.cc + 0002_TribonacciNumber.cc + +) + +# Create a library target +add_library(0005DYNAMICPROGRAMMING ${0005DYNAMICPROGRAMMING_SOURCES}) \ No newline at end of file diff --git a/test/0005_DynamicProgramming/0001_FibonacciNumberTest.cc b/test/0005_DynamicProgramming/0001_FibonacciNumberTest.cc new file mode 100644 index 0000000..844e3ae --- /dev/null +++ b/test/0005_DynamicProgramming/0001_FibonacciNumberTest.cc @@ -0,0 +1,34 @@ +#include +#include "../../include/0005_DynamicProgramming/0001_FibonacciNumber.h" +using namespace std; + +namespace FibonacciNumber +{ + TEST(FibonacciNumber, RecursiveTest) + { + // Arrange + DynamicProgramming dp; + int n = 5; + int expectedFib = 5; + + // Act + int actualFib = dp.RecursiveNthFibonacci(n); + + // Assert + ASSERT_EQ(expectedFib, actualFib); + } + + TEST(FibonacciNumber, DpTest) + { + // Arrange + DynamicProgramming dp; + int n = 5; + int expectedFib = 5; + + // Act + int actualFib = dp.DpNthFibonacci(n); + + // Assert + ASSERT_EQ(expectedFib, actualFib); + } +} \ No newline at end of file diff --git a/test/0005_DynamicProgramming/0002_TribonacciNumberTest.cc b/test/0005_DynamicProgramming/0002_TribonacciNumberTest.cc new file mode 100644 index 0000000..d7cd6ae --- /dev/null +++ b/test/0005_DynamicProgramming/0002_TribonacciNumberTest.cc @@ -0,0 +1,34 @@ +#include +#include "../../include/0005_DynamicProgramming/0002_TribonacciNumber.h" +using namespace std; + +namespace TribonacciNumber +{ + TEST(TribonacciNumber, RecursiveTest) + { + // Arrange + DynamicProgramming dp; + int n = 5; + int expectedFib = 2; + + // Act + int actualFib = dp.RecursiveNthTribonacci(n); + + // Assert + ASSERT_EQ(expectedFib, actualFib); + } + + TEST(TribonacciNumber, DpTest) + { + // Arrange + DynamicProgramming dp; + int n = 10; + int expectedFib = 44; + + // Act + int actualFib = dp.DpNthTribonacci(n); + + // Assert + ASSERT_EQ(expectedFib, actualFib); + } +} \ No newline at end of file diff --git a/test/0005_DynamicProgramming/CMakeLists.txt b/test/0005_DynamicProgramming/CMakeLists.txt index e69de29..07c34b1 100644 --- a/test/0005_DynamicProgramming/CMakeLists.txt +++ b/test/0005_DynamicProgramming/CMakeLists.txt @@ -0,0 +1,34 @@ +cmake_policy(SET CMP0135 NEW) + +include(FetchContent) +FetchContent_Declare( + googletest + URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip +) + +# For Windows: Prevent overriding the parent project's compiler/linker settings +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) +FetchContent_MakeAvailable(googletest) + +enable_testing() + +add_executable( + 0005DynamicProgrammingTests + 0001_FibonacciNumberTest.cc + 0002_TribonacciNumberTest.cc + +) + +target_link_libraries( + 0005DynamicProgrammingTests + GTest::gtest_main + 0005DYNAMICPROGRAMMING +) + +# Add .clang-tidy configuration to this library. +if(CLANG_TIDY_EXE) + set_target_properties(0005DYNAMICPROGRAMMING PROPERTIES CXX_CLANG_TIDY "${CLANG_TIDY_COMMAND}") +endif() + +include(GoogleTest) +gtest_discover_tests(0005DynamicProgrammingTests DISCOVERY_TIMEOUT 30) \ No newline at end of file From b707e7c2ccbbe2070274449f86eb5fea1b9afd5d Mon Sep 17 00:00:00 2001 From: Debashis Nandi Date: Sat, 2 Aug 2025 23:56:12 +0530 Subject: [PATCH 02/10] feature: climbing stairs added --- .../0003_ClimbingStairs.h | 24 ++++++++++++++ .../0003_ClimbingStairs.cc | 29 ++++++++++++++++ source/0005_DynamicProgramming/CMakeLists.txt | 1 + .../0003_ClimbingStairsTest.cc | 33 +++++++++++++++++++ test/0005_DynamicProgramming/CMakeLists.txt | 1 + 5 files changed, 88 insertions(+) create mode 100644 include/0005_DynamicProgramming/0003_ClimbingStairs.h create mode 100644 source/0005_DynamicProgramming/0003_ClimbingStairs.cc create mode 100644 test/0005_DynamicProgramming/0003_ClimbingStairsTest.cc diff --git a/include/0005_DynamicProgramming/0003_ClimbingStairs.h b/include/0005_DynamicProgramming/0003_ClimbingStairs.h new file mode 100644 index 0000000..84c5f81 --- /dev/null +++ b/include/0005_DynamicProgramming/0003_ClimbingStairs.h @@ -0,0 +1,24 @@ +#pragma once +#include +using namespace std; + +/* +Pattern 1 +Linear Recurrence + +Description +There are n stairs, and a person standing at the bottom wants to climb stairs to reach the top. +The person can climb either 1 stair or 2 stairs at a time, the task is to count the number of ways that a person can reach at the top. + +*/ + +namespace ClimbingStairs +{ + class DynamicProgramming + { + private: + public: + int RecursiveCountWays(int n); + int DpCountWays(int n); + }; +} \ No newline at end of file diff --git a/source/0005_DynamicProgramming/0003_ClimbingStairs.cc b/source/0005_DynamicProgramming/0003_ClimbingStairs.cc new file mode 100644 index 0000000..d832841 --- /dev/null +++ b/source/0005_DynamicProgramming/0003_ClimbingStairs.cc @@ -0,0 +1,29 @@ +#include "../../include/0005_DynamicProgramming/0003_ClimbingStairs.h" +using namespace std; + +namespace ClimbingStairs +{ + int DynamicProgramming::RecursiveCountWays(int n) + { + if (n == 0 || n == 1) + { + return 1; + } + + return this->RecursiveCountWays(n - 1) + this->RecursiveCountWays(n - 2); + } + + int DynamicProgramming::DpCountWays(int n) + { + vector dp(n + 1, 0); + dp[0] = 1; + dp[1] = 1; + + for (int i = 2; i <= n; i++) + { + dp[i] = dp[i - 1] + dp[i - 2]; + } + + return dp[n]; + } +} \ No newline at end of file diff --git a/source/0005_DynamicProgramming/CMakeLists.txt b/source/0005_DynamicProgramming/CMakeLists.txt index c0a26ef..c62c512 100644 --- a/source/0005_DynamicProgramming/CMakeLists.txt +++ b/source/0005_DynamicProgramming/CMakeLists.txt @@ -2,6 +2,7 @@ set(0005DYNAMICPROGRAMMING_SOURCES 0001_FibonacciNumber.cc 0002_TribonacciNumber.cc + 0003_ClimbingStairs.cc ) diff --git a/test/0005_DynamicProgramming/0003_ClimbingStairsTest.cc b/test/0005_DynamicProgramming/0003_ClimbingStairsTest.cc new file mode 100644 index 0000000..995a828 --- /dev/null +++ b/test/0005_DynamicProgramming/0003_ClimbingStairsTest.cc @@ -0,0 +1,33 @@ +#include +#include "../../include/0005_DynamicProgramming/0003_ClimbingStairs.h" + +namespace ClimbingStairs +{ + TEST(ClimbingStairs, RecursiveTest) + { + // Arrange + DynamicProgramming dp; + int n = 4; + int expectedCount = 5; + + // Act + int actualCount = dp.RecursiveCountWays(n); + + // Assert + ASSERT_EQ(expectedCount, actualCount); + } + + TEST(ClimbingStairs, DpTest) + { + // Arrange + DynamicProgramming dp; + int n = 4; + int expectedCount = 5; + + // Act + int actualCount = dp.DpCountWays(n); + + // Assert + ASSERT_EQ(expectedCount, actualCount); + } +} \ No newline at end of file diff --git a/test/0005_DynamicProgramming/CMakeLists.txt b/test/0005_DynamicProgramming/CMakeLists.txt index 07c34b1..e00e424 100644 --- a/test/0005_DynamicProgramming/CMakeLists.txt +++ b/test/0005_DynamicProgramming/CMakeLists.txt @@ -16,6 +16,7 @@ add_executable( 0005DynamicProgrammingTests 0001_FibonacciNumberTest.cc 0002_TribonacciNumberTest.cc + 0003_ClimbingStairsTest.cc ) From addca6e61b67df90ffd3cda11f8ea22e872dbae0 Mon Sep 17 00:00:00 2001 From: Debashis Nandi Date: Sun, 3 Aug 2025 21:37:16 +0530 Subject: [PATCH 03/10] feature: min cost climbing added --- .../0004_MinimumCostClimbingStairs.h | 25 ++++++++++ .../0004_MinimumCostClimbingStairs.cc | 48 +++++++++++++++++++ source/0005_DynamicProgramming/CMakeLists.txt | 1 + .../0004_MinimumCostClimbingStairsTest.cc | 33 +++++++++++++ test/0005_DynamicProgramming/CMakeLists.txt | 1 + 5 files changed, 108 insertions(+) create mode 100644 include/0005_DynamicProgramming/0004_MinimumCostClimbingStairs.h create mode 100644 source/0005_DynamicProgramming/0004_MinimumCostClimbingStairs.cc create mode 100644 test/0005_DynamicProgramming/0004_MinimumCostClimbingStairsTest.cc diff --git a/include/0005_DynamicProgramming/0004_MinimumCostClimbingStairs.h b/include/0005_DynamicProgramming/0004_MinimumCostClimbingStairs.h new file mode 100644 index 0000000..26b1990 --- /dev/null +++ b/include/0005_DynamicProgramming/0004_MinimumCostClimbingStairs.h @@ -0,0 +1,25 @@ +#pragma once +#include +using namespace std; + +/* +Pattern 1 +Linear Recurrence + +Description +Given an array of integers cost[] of length n, where cost[i] is the cost of the ith step on a staircase. Once the cost is paid, we can either climb 1 or 2 steps. +We can either start from the step with index 0, or the step with index 1. The task is to find the minimum cost to reach the top. + +*/ + +namespace MinimumCostClimbingStairs +{ + class DynamicProgramming + { + private: + int MinCostRecursive(int step, vector& cost); + public: + int RecursiveMinimumCostClimbingStairs(vector& cost); + int DpMinimumCostClimbingStairs(vector& cost); + }; +} \ No newline at end of file diff --git a/source/0005_DynamicProgramming/0004_MinimumCostClimbingStairs.cc b/source/0005_DynamicProgramming/0004_MinimumCostClimbingStairs.cc new file mode 100644 index 0000000..d364000 --- /dev/null +++ b/source/0005_DynamicProgramming/0004_MinimumCostClimbingStairs.cc @@ -0,0 +1,48 @@ +#include "../../include/0005_DynamicProgramming/0004_MinimumCostClimbingStairs.h" +#include + +namespace MinimumCostClimbingStairs +{ + int DynamicProgramming::MinCostRecursive(int step, vector& cost) + { + if (step == 0 || step == 1) + { + return cost[step]; + } + + return cost[step] + min(this->MinCostRecursive(step - 1, cost), this->MinCostRecursive(step - 2, cost)); + } + + int DynamicProgramming::RecursiveMinimumCostClimbingStairs(vector& cost) + { + int totalSteps = cost.size(); + + if (totalSteps == 1) + { + return cost[0]; + } + + return min(this->MinCostRecursive(totalSteps - 1, cost), this->MinCostRecursive(totalSteps - 2, cost)); + } + + int DynamicProgramming::DpMinimumCostClimbingStairs(vector& cost) + { + int totalSteps = cost.size(); + vector dp(totalSteps, 0); + + if (totalSteps == 1) + { + return cost[0]; + } + + dp[0] = cost[0]; + dp[1] = cost[1]; + + for (int i = 2; i < totalSteps; i++) + { + dp[i] = cost[i] + min(dp[i - 1], dp[i - 2]); + } + + return min(dp[totalSteps - 1], dp[totalSteps - 2]); + } +} \ No newline at end of file diff --git a/source/0005_DynamicProgramming/CMakeLists.txt b/source/0005_DynamicProgramming/CMakeLists.txt index c62c512..3f1d08c 100644 --- a/source/0005_DynamicProgramming/CMakeLists.txt +++ b/source/0005_DynamicProgramming/CMakeLists.txt @@ -3,6 +3,7 @@ set(0005DYNAMICPROGRAMMING_SOURCES 0001_FibonacciNumber.cc 0002_TribonacciNumber.cc 0003_ClimbingStairs.cc + 0004_MinimumCostClimbingStairs.cc ) diff --git a/test/0005_DynamicProgramming/0004_MinimumCostClimbingStairsTest.cc b/test/0005_DynamicProgramming/0004_MinimumCostClimbingStairsTest.cc new file mode 100644 index 0000000..84eb7e7 --- /dev/null +++ b/test/0005_DynamicProgramming/0004_MinimumCostClimbingStairsTest.cc @@ -0,0 +1,33 @@ +#include +#include "../../include/0005_DynamicProgramming/0004_MinimumCostClimbingStairs.h" + +namespace MinimumCostClimbingStairs +{ + TEST(MinimumCostClimbingStairs, RecursionTest) + { + // Arrange + DynamicProgramming dp; + vector cost = { 16, 19, 10, 12, 18 }; + int expectedCost = 31; + + // Act + int actualCost = dp.RecursiveMinimumCostClimbingStairs(cost); + + // Assert + ASSERT_EQ(expectedCost, actualCost); + } + + TEST(MinimumCostClimbingStairs, DpTest) + { + // Arrange + DynamicProgramming dp; + vector cost = { 16, 19, 10, 12, 18 }; + int expectedCost = 31; + + // Act + int actualCost = dp.DpMinimumCostClimbingStairs(cost); + + // Assert + ASSERT_EQ(expectedCost, actualCost); + } +} \ No newline at end of file diff --git a/test/0005_DynamicProgramming/CMakeLists.txt b/test/0005_DynamicProgramming/CMakeLists.txt index e00e424..57447d2 100644 --- a/test/0005_DynamicProgramming/CMakeLists.txt +++ b/test/0005_DynamicProgramming/CMakeLists.txt @@ -17,6 +17,7 @@ add_executable( 0001_FibonacciNumberTest.cc 0002_TribonacciNumberTest.cc 0003_ClimbingStairsTest.cc + 0004_MinimumCostClimbingStairsTest.cc ) From ea193daa7f302b3ec30743e7f0b4f650e25ba243 Mon Sep 17 00:00:00 2001 From: Debashis Nandi Date: Wed, 6 Aug 2025 23:04:26 +0530 Subject: [PATCH 04/10] feature: house robber 1 solution added --- .../0005_HouseRobber1.h | 25 +++++++++++ .../0005_HouseRobber1.cc | 44 +++++++++++++++++++ source/0005_DynamicProgramming/CMakeLists.txt | 1 + .../0005_HouseRobber1Test.cc | 33 ++++++++++++++ test/0005_DynamicProgramming/CMakeLists.txt | 1 + 5 files changed, 104 insertions(+) create mode 100644 include/0005_DynamicProgramming/0005_HouseRobber1.h create mode 100644 source/0005_DynamicProgramming/0005_HouseRobber1.cc create mode 100644 test/0005_DynamicProgramming/0005_HouseRobber1Test.cc diff --git a/include/0005_DynamicProgramming/0005_HouseRobber1.h b/include/0005_DynamicProgramming/0005_HouseRobber1.h new file mode 100644 index 0000000..b348a43 --- /dev/null +++ b/include/0005_DynamicProgramming/0005_HouseRobber1.h @@ -0,0 +1,25 @@ +#pragma once +#include +using namespace std; + +/* +Pattern 1 +Linear Recurrence + +Description +There are n houses built in a line, each of which contains some money in it. +A robber wants to steal money from these houses, but he can’t steal from two adjacent houses. The task is to find the maximum amount of money which can be stolen. + +*/ + +namespace HouseRobber1 +{ + class DynamicProgramming + { + private: + int MaxLootRecursive(int house, vector& houseValues); + public: + int RecursiveMaximumLoot(vector& houseValues); + int DpMaximumLoot(vector& houseValues); + }; +} \ No newline at end of file diff --git a/source/0005_DynamicProgramming/0005_HouseRobber1.cc b/source/0005_DynamicProgramming/0005_HouseRobber1.cc new file mode 100644 index 0000000..e9c20dc --- /dev/null +++ b/source/0005_DynamicProgramming/0005_HouseRobber1.cc @@ -0,0 +1,44 @@ +#include "../../include/0005_DynamicProgramming/0005_HouseRobber1.h" + +namespace HouseRobber1 +{ + int DynamicProgramming::MaxLootRecursive(int house, vector& houseValues) + { + if (house <= 0) + { + return 0; + } + + if (house == 1) + { + return houseValues[0]; + } + + int pickCurrentHouse = houseValues[house - 1] + this->MaxLootRecursive(house - 2, houseValues); + int dropCurrentHouse = this->MaxLootRecursive(house - 1, houseValues); + + return max(pickCurrentHouse, dropCurrentHouse); + } + + int DynamicProgramming::RecursiveMaximumLoot(vector& houseValues) + { + int totalNumberOfHouses = houseValues.size(); + return this->MaxLootRecursive(totalNumberOfHouses, houseValues); + } + + int DynamicProgramming::DpMaximumLoot(vector& houseValues) + { + int totalNumberOfHouses = houseValues.size(); + vector dp(totalNumberOfHouses + 1, 0); + + dp[0] = 0; + dp[1] = houseValues[0]; + + for (int i = 2; i <= totalNumberOfHouses; i++) + { + dp[i] = max(dp[i - 2] + houseValues[i - 1], dp[i - 1]); + } + + return dp[totalNumberOfHouses]; + } +} \ No newline at end of file diff --git a/source/0005_DynamicProgramming/CMakeLists.txt b/source/0005_DynamicProgramming/CMakeLists.txt index 3f1d08c..1f82b05 100644 --- a/source/0005_DynamicProgramming/CMakeLists.txt +++ b/source/0005_DynamicProgramming/CMakeLists.txt @@ -4,6 +4,7 @@ set(0005DYNAMICPROGRAMMING_SOURCES 0002_TribonacciNumber.cc 0003_ClimbingStairs.cc 0004_MinimumCostClimbingStairs.cc + 0005_HouseRobber1.cc ) diff --git a/test/0005_DynamicProgramming/0005_HouseRobber1Test.cc b/test/0005_DynamicProgramming/0005_HouseRobber1Test.cc new file mode 100644 index 0000000..8b27601 --- /dev/null +++ b/test/0005_DynamicProgramming/0005_HouseRobber1Test.cc @@ -0,0 +1,33 @@ +#include +#include "../../include/0005_DynamicProgramming/0005_HouseRobber1.h" + +namespace HouseRobber1 +{ + TEST(HouseRobber1, RecursionTest) + { + // Arrange + DynamicProgramming dp; + vector houseValues = { 6, 7, 1, 3, 8, 2, 4 }; + int expectedMaximumLoot = 19; + + // Act + int actualMaximumLoot = dp.RecursiveMaximumLoot(houseValues); + + // Assert + ASSERT_EQ(expectedMaximumLoot, actualMaximumLoot); + } + + TEST(HouseRobber1, DpTest) + { + // Arrange + DynamicProgramming dp; + vector houseValues = { 6, 7, 1, 3, 8, 2, 4 }; + int expectedMaximumLoot = 19; + + // Act + int actualMaximumLoot = dp.DpMaximumLoot(houseValues); + + // Assert + ASSERT_EQ(expectedMaximumLoot, actualMaximumLoot); + } +} \ No newline at end of file diff --git a/test/0005_DynamicProgramming/CMakeLists.txt b/test/0005_DynamicProgramming/CMakeLists.txt index 57447d2..fb6723a 100644 --- a/test/0005_DynamicProgramming/CMakeLists.txt +++ b/test/0005_DynamicProgramming/CMakeLists.txt @@ -18,6 +18,7 @@ add_executable( 0002_TribonacciNumberTest.cc 0003_ClimbingStairsTest.cc 0004_MinimumCostClimbingStairsTest.cc + 0005_HouseRobber1Test.cc ) From 562de8b3b9b772e9adf08bd04c8fccabde7daf1f Mon Sep 17 00:00:00 2001 From: Debashis Nandi Date: Thu, 7 Aug 2025 21:57:49 +0530 Subject: [PATCH 05/10] fix: size_t warning fix, house robber 2 init --- .../0004_MinimumCostClimbingStairs.h | 2 +- include/0005_DynamicProgramming/0005_HouseRobber1.h | 2 +- include/0005_DynamicProgramming/0006_HouseRobber2.h | 0 .../0004_MinimumCostClimbingStairs.cc | 8 ++++---- source/0005_DynamicProgramming/0005_HouseRobber1.cc | 8 ++++---- source/0005_DynamicProgramming/0006_HouseRobber2.cc | 0 source/0005_DynamicProgramming/CMakeLists.txt | 1 + test/0005_DynamicProgramming/0006_HouseRobber2Test.cc | 0 test/0005_DynamicProgramming/CMakeLists.txt | 1 + 9 files changed, 12 insertions(+), 10 deletions(-) create mode 100644 include/0005_DynamicProgramming/0006_HouseRobber2.h create mode 100644 source/0005_DynamicProgramming/0006_HouseRobber2.cc create mode 100644 test/0005_DynamicProgramming/0006_HouseRobber2Test.cc diff --git a/include/0005_DynamicProgramming/0004_MinimumCostClimbingStairs.h b/include/0005_DynamicProgramming/0004_MinimumCostClimbingStairs.h index 26b1990..3719706 100644 --- a/include/0005_DynamicProgramming/0004_MinimumCostClimbingStairs.h +++ b/include/0005_DynamicProgramming/0004_MinimumCostClimbingStairs.h @@ -17,7 +17,7 @@ namespace MinimumCostClimbingStairs class DynamicProgramming { private: - int MinCostRecursive(int step, vector& cost); + int MinCostRecursive(size_t step, vector& cost); public: int RecursiveMinimumCostClimbingStairs(vector& cost); int DpMinimumCostClimbingStairs(vector& cost); diff --git a/include/0005_DynamicProgramming/0005_HouseRobber1.h b/include/0005_DynamicProgramming/0005_HouseRobber1.h index b348a43..8851000 100644 --- a/include/0005_DynamicProgramming/0005_HouseRobber1.h +++ b/include/0005_DynamicProgramming/0005_HouseRobber1.h @@ -17,7 +17,7 @@ namespace HouseRobber1 class DynamicProgramming { private: - int MaxLootRecursive(int house, vector& houseValues); + int MaxLootRecursive(size_t house, vector& houseValues); public: int RecursiveMaximumLoot(vector& houseValues); int DpMaximumLoot(vector& houseValues); diff --git a/include/0005_DynamicProgramming/0006_HouseRobber2.h b/include/0005_DynamicProgramming/0006_HouseRobber2.h new file mode 100644 index 0000000..e69de29 diff --git a/source/0005_DynamicProgramming/0004_MinimumCostClimbingStairs.cc b/source/0005_DynamicProgramming/0004_MinimumCostClimbingStairs.cc index d364000..1f244ac 100644 --- a/source/0005_DynamicProgramming/0004_MinimumCostClimbingStairs.cc +++ b/source/0005_DynamicProgramming/0004_MinimumCostClimbingStairs.cc @@ -3,7 +3,7 @@ namespace MinimumCostClimbingStairs { - int DynamicProgramming::MinCostRecursive(int step, vector& cost) + int DynamicProgramming::MinCostRecursive(size_t step, vector& cost) { if (step == 0 || step == 1) { @@ -15,7 +15,7 @@ namespace MinimumCostClimbingStairs int DynamicProgramming::RecursiveMinimumCostClimbingStairs(vector& cost) { - int totalSteps = cost.size(); + size_t totalSteps = cost.size(); if (totalSteps == 1) { @@ -27,7 +27,7 @@ namespace MinimumCostClimbingStairs int DynamicProgramming::DpMinimumCostClimbingStairs(vector& cost) { - int totalSteps = cost.size(); + size_t totalSteps = cost.size(); vector dp(totalSteps, 0); if (totalSteps == 1) @@ -38,7 +38,7 @@ namespace MinimumCostClimbingStairs dp[0] = cost[0]; dp[1] = cost[1]; - for (int i = 2; i < totalSteps; i++) + for (size_t i = 2; i < totalSteps; i++) { dp[i] = cost[i] + min(dp[i - 1], dp[i - 2]); } diff --git a/source/0005_DynamicProgramming/0005_HouseRobber1.cc b/source/0005_DynamicProgramming/0005_HouseRobber1.cc index e9c20dc..3948fdf 100644 --- a/source/0005_DynamicProgramming/0005_HouseRobber1.cc +++ b/source/0005_DynamicProgramming/0005_HouseRobber1.cc @@ -2,7 +2,7 @@ namespace HouseRobber1 { - int DynamicProgramming::MaxLootRecursive(int house, vector& houseValues) + int DynamicProgramming::MaxLootRecursive(size_t house, vector& houseValues) { if (house <= 0) { @@ -22,19 +22,19 @@ namespace HouseRobber1 int DynamicProgramming::RecursiveMaximumLoot(vector& houseValues) { - int totalNumberOfHouses = houseValues.size(); + size_t totalNumberOfHouses = houseValues.size(); return this->MaxLootRecursive(totalNumberOfHouses, houseValues); } int DynamicProgramming::DpMaximumLoot(vector& houseValues) { - int totalNumberOfHouses = houseValues.size(); + size_t totalNumberOfHouses = houseValues.size(); vector dp(totalNumberOfHouses + 1, 0); dp[0] = 0; dp[1] = houseValues[0]; - for (int i = 2; i <= totalNumberOfHouses; i++) + for (size_t i = 2; i <= totalNumberOfHouses; i++) { dp[i] = max(dp[i - 2] + houseValues[i - 1], dp[i - 1]); } diff --git a/source/0005_DynamicProgramming/0006_HouseRobber2.cc b/source/0005_DynamicProgramming/0006_HouseRobber2.cc new file mode 100644 index 0000000..e69de29 diff --git a/source/0005_DynamicProgramming/CMakeLists.txt b/source/0005_DynamicProgramming/CMakeLists.txt index 1f82b05..2f793df 100644 --- a/source/0005_DynamicProgramming/CMakeLists.txt +++ b/source/0005_DynamicProgramming/CMakeLists.txt @@ -5,6 +5,7 @@ set(0005DYNAMICPROGRAMMING_SOURCES 0003_ClimbingStairs.cc 0004_MinimumCostClimbingStairs.cc 0005_HouseRobber1.cc + 0006_HouseRobber2.cc ) diff --git a/test/0005_DynamicProgramming/0006_HouseRobber2Test.cc b/test/0005_DynamicProgramming/0006_HouseRobber2Test.cc new file mode 100644 index 0000000..e69de29 diff --git a/test/0005_DynamicProgramming/CMakeLists.txt b/test/0005_DynamicProgramming/CMakeLists.txt index fb6723a..f6361cb 100644 --- a/test/0005_DynamicProgramming/CMakeLists.txt +++ b/test/0005_DynamicProgramming/CMakeLists.txt @@ -19,6 +19,7 @@ add_executable( 0003_ClimbingStairsTest.cc 0004_MinimumCostClimbingStairsTest.cc 0005_HouseRobber1Test.cc + 0006_HouseRobber2Test.cc ) From 0adefcb3dfddd9851696200ab5ad11d4b81a93e0 Mon Sep 17 00:00:00 2001 From: Debashis Nandi Date: Thu, 14 Aug 2025 01:44:28 +0530 Subject: [PATCH 06/10] feature: dp house robber 2 solution added --- .../0006_HouseRobber2.h | 28 ++++++ .../0006_HouseRobber2.cc | 95 +++++++++++++++++++ .../0006_HouseRobber2Test.cc | 48 ++++++++++ 3 files changed, 171 insertions(+) diff --git a/include/0005_DynamicProgramming/0006_HouseRobber2.h b/include/0005_DynamicProgramming/0006_HouseRobber2.h index e69de29..e030106 100644 --- a/include/0005_DynamicProgramming/0006_HouseRobber2.h +++ b/include/0005_DynamicProgramming/0006_HouseRobber2.h @@ -0,0 +1,28 @@ +#pragma once +#include +using namespace std; + +/* +Pattern 1 +Linear Recurrence + +Description +You are given an array arr[] which represents houses arranged in a circle, where each house has a certain value. A thief aims to maximize the total stolen value without robbing two adjacent houses. +Determine the maximum amount the thief can steal. + +Note: Since the houses are in a circle, the first and last houses are also considered adjacent. + +*/ + +namespace HouseRobber2 +{ + class DynamicProgramming + { + private: + int MaxLootRecursive(size_t house, vector& houseValues); + int MaxLootDp(size_t firstHouse, size_t lastHouse, vector& houseValues); + public: + int RecursiveMaximumLoot(vector& houseValues); + int DpMaximumLoot(vector& houseValues); + }; +} \ No newline at end of file diff --git a/source/0005_DynamicProgramming/0006_HouseRobber2.cc b/source/0005_DynamicProgramming/0006_HouseRobber2.cc index e69de29..4068398 100644 --- a/source/0005_DynamicProgramming/0006_HouseRobber2.cc +++ b/source/0005_DynamicProgramming/0006_HouseRobber2.cc @@ -0,0 +1,95 @@ +#include "../../include/0005_DynamicProgramming/0006_HouseRobber2.h" + +namespace HouseRobber2 +{ + int DynamicProgramming::MaxLootRecursive(size_t house, vector& houseValues) + { + if (house <= 0) + { + return 0; + } + + if (house == 1) + { + return houseValues[0]; + } + + int pickCurrentHouse = houseValues[house - 1] + this->MaxLootRecursive(house - 2, houseValues); + int dropCurrentHouse = this->MaxLootRecursive(house - 1, houseValues); + + return max(pickCurrentHouse, dropCurrentHouse); + } + + int DynamicProgramming::MaxLootDp(size_t firstHouse, size_t lastHouse, vector& houseValues) + { + int totalNumberOfHouses = lastHouse - firstHouse + 1; + + if (totalNumberOfHouses == 0) + { + return 0; + } + + if (totalNumberOfHouses == 1) + { + return houseValues[firstHouse]; + } + + vector dp(totalNumberOfHouses, 0); + + dp[0] = houseValues[firstHouse]; + dp[1] = max(houseValues[firstHouse], houseValues[firstHouse + 1]); + + for (size_t i = 2; i < totalNumberOfHouses; i++) + { + dp[i] = max(houseValues[firstHouse + i] + dp[i - 2], dp[i - 1]); + } + + return dp[totalNumberOfHouses - 1]; + } + + int DynamicProgramming::RecursiveMaximumLoot(vector& houseValues) + { + if (houseValues.size() == 0) + { + return 0; + } + + if (houseValues.size() == 1) + { + return houseValues[0]; + } + + size_t totalNumberOfHouses = houseValues.size()-1; + + // Case 1: Exclude last house. + vector pickFirstHouse(houseValues.begin(), houseValues.end() - 1); + + // Case 2: Exlcude first house. + vector pickLastHouse(houseValues.begin() + 1, houseValues.end()); + + return max(this->MaxLootRecursive(totalNumberOfHouses, pickFirstHouse), this->MaxLootRecursive(totalNumberOfHouses, pickLastHouse)); + } + + int DynamicProgramming::DpMaximumLoot(vector& houseValues) + { + size_t totalNumberOfHouses = houseValues.size(); + + if (totalNumberOfHouses == 0) + { + return 0; + } + + if (totalNumberOfHouses == 1) + { + return houseValues[0]; + } + + // Case 1: Exclude last house. + int pickFirstHouse = this->MaxLootDp(0, totalNumberOfHouses - 2, houseValues); + + // Case 2: Exlcude first house. + int pickLastHouse = this->MaxLootDp(1, totalNumberOfHouses - 1, houseValues); + + return max(pickFirstHouse, pickLastHouse); + } +} \ No newline at end of file diff --git a/test/0005_DynamicProgramming/0006_HouseRobber2Test.cc b/test/0005_DynamicProgramming/0006_HouseRobber2Test.cc index e69de29..ec29b35 100644 --- a/test/0005_DynamicProgramming/0006_HouseRobber2Test.cc +++ b/test/0005_DynamicProgramming/0006_HouseRobber2Test.cc @@ -0,0 +1,48 @@ +#include +#include "../../include/0005_DynamicProgramming/0006_HouseRobber2.h" + +namespace HouseRobber2 +{ + TEST(HouseRobber2, RecursionTest01) + { + // Arrange + DynamicProgramming dp; + vector houseValues = { 2, 2, 3, 1, 2 }; + int expectedMaximumLoot = 5; + + // Act + int actualMaximumLoot = dp.RecursiveMaximumLoot(houseValues); + + // Assert + ASSERT_EQ(expectedMaximumLoot, actualMaximumLoot); + } + + TEST(HouseRobber2, DpTest01) + { + // Arrange + DynamicProgramming dp; + vector houseValues = { 2, 2, 3, 1, 2 }; + int expectedMaximumLoot = 5; + + // Act + int actualMaximumLoot = dp.DpMaximumLoot(houseValues); + + // Assert + ASSERT_EQ(expectedMaximumLoot, actualMaximumLoot); + } + + TEST(HouseRobber2, DpTest02) + { + // Arrange + DynamicProgramming dp; + vector houseValues = { 9, 1, 8, 2 }; + int expectedMaximumLoot = 17; + + // Act + int actualMaximumLoot = dp.DpMaximumLoot(houseValues); + + // Assert + ASSERT_EQ(expectedMaximumLoot, actualMaximumLoot); + } + +} \ No newline at end of file From d17904cfe5dda6ed52b4d80fec5b8b0aa3fd9ba5 Mon Sep 17 00:00:00 2001 From: Debashis Nandi Date: Fri, 19 Sep 2025 21:25:46 +0530 Subject: [PATCH 07/10] feature: decode ways init --- .../0005_DynamicProgramming/0007_DecodeWays.h | 31 +++++++++++++++++++ .../0007_DecodeWays.cc | 19 ++++++++++++ source/0005_DynamicProgramming/CMakeLists.txt | 1 + .../0007_DecodeWaysTest.cc | 0 test/0005_DynamicProgramming/CMakeLists.txt | 1 + 5 files changed, 52 insertions(+) create mode 100644 include/0005_DynamicProgramming/0007_DecodeWays.h create mode 100644 source/0005_DynamicProgramming/0007_DecodeWays.cc create mode 100644 test/0005_DynamicProgramming/0007_DecodeWaysTest.cc diff --git a/include/0005_DynamicProgramming/0007_DecodeWays.h b/include/0005_DynamicProgramming/0007_DecodeWays.h new file mode 100644 index 0000000..a3591bb --- /dev/null +++ b/include/0005_DynamicProgramming/0007_DecodeWays.h @@ -0,0 +1,31 @@ +#pragma once + +#include +#include +using namespace std; + +/* +Pattern 1 +Linear Recurrence + +Description +Let 1 maps to 'A', 2 maps to 'B', ..., 26 to 'Z'.Given a digit sequence, count the number of possible decodings of the given digit sequence. + +Consider the input string "123".There are three valid ways to decode it : +"ABC" : The grouping is(1, 2, 3) -> 'A', 'B', 'C' +"AW" : The grouping is(1, 23) -> 'A', 'W' +"LC" : The grouping is(12, 3) -> 'L', 'C' +Note : Groupings that contain invalid codes(e.g., "0" by itself or numbers greater than "26") are not allowed. +For instance, the string "230" is invalid because "0" cannot stand alone, and "30" is greater than "26", so it cannot represent any letter.The task is to find the total number of valid ways to decode a given string. +*/ + +namespace DecodeWays +{ + class DynamicProgramming + { + private: + int CountWaysRecursiveHelper(string& digits, size_t index); + public: + int RecursiveCountWays(string digits); + }; +} \ No newline at end of file diff --git a/source/0005_DynamicProgramming/0007_DecodeWays.cc b/source/0005_DynamicProgramming/0007_DecodeWays.cc new file mode 100644 index 0000000..8b189bc --- /dev/null +++ b/source/0005_DynamicProgramming/0007_DecodeWays.cc @@ -0,0 +1,19 @@ +#include "../../include/0005_DynamicProgramming/0007_DecodeWays.h" + +namespace DecodeWays +{ + int DynamicProgramming::CountWaysRecursiveHelper(string& digits, size_t index) + { + size_t digitsLength = digits.size(); + + if (index >= digitsLength) + { + return 1; + } + } + + int DynamicProgramming::RecursiveCountWays(string digits) + { + + } +} \ No newline at end of file diff --git a/source/0005_DynamicProgramming/CMakeLists.txt b/source/0005_DynamicProgramming/CMakeLists.txt index 2f793df..c8a87c5 100644 --- a/source/0005_DynamicProgramming/CMakeLists.txt +++ b/source/0005_DynamicProgramming/CMakeLists.txt @@ -6,6 +6,7 @@ set(0005DYNAMICPROGRAMMING_SOURCES 0004_MinimumCostClimbingStairs.cc 0005_HouseRobber1.cc 0006_HouseRobber2.cc + 0007_DecodeWays.cc ) diff --git a/test/0005_DynamicProgramming/0007_DecodeWaysTest.cc b/test/0005_DynamicProgramming/0007_DecodeWaysTest.cc new file mode 100644 index 0000000..e69de29 diff --git a/test/0005_DynamicProgramming/CMakeLists.txt b/test/0005_DynamicProgramming/CMakeLists.txt index f6361cb..2e44016 100644 --- a/test/0005_DynamicProgramming/CMakeLists.txt +++ b/test/0005_DynamicProgramming/CMakeLists.txt @@ -20,6 +20,7 @@ add_executable( 0004_MinimumCostClimbingStairsTest.cc 0005_HouseRobber1Test.cc 0006_HouseRobber2Test.cc + 0007_DecodeWaysTest.cc ) From b2c83c445712f8500c19c26627dc2377fd4bb52e Mon Sep 17 00:00:00 2001 From: Debashis Nandi Date: Mon, 13 Oct 2025 02:03:01 +0530 Subject: [PATCH 08/10] feature: decode ways recursive sol added --- .../0007_DecodeWays.cc | 18 +++++++++++++++++- .../0007_DecodeWaysTest.cc | 19 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/source/0005_DynamicProgramming/0007_DecodeWays.cc b/source/0005_DynamicProgramming/0007_DecodeWays.cc index 8b189bc..a4c1561 100644 --- a/source/0005_DynamicProgramming/0007_DecodeWays.cc +++ b/source/0005_DynamicProgramming/0007_DecodeWays.cc @@ -6,14 +6,30 @@ namespace DecodeWays { size_t digitsLength = digits.size(); + // Base case: If the end of the string is reached, return 1 as it signifies a valid decoding. if (index >= digitsLength) { return 1; } + + int ways = 0; + + // Single digit decoding: check if current digit is not '0'. + if (digits[index] != '0') + { + ways = this->CountWaysRecursiveHelper(digits, index + 1); + } + + // Two digit decoding: check if next two digits are valid. + if ((index + 1 < digitsLength) && ((digits[index] == '1' && digits[index + 1] <= '9') || (digits[index] == '2' && digits[index + 1] <= '6'))) + { + ways += this->CountWaysRecursiveHelper(digits, index + 2); + } + return ways; } int DynamicProgramming::RecursiveCountWays(string digits) { - + return this->CountWaysRecursiveHelper(digits, 0); } } \ No newline at end of file diff --git a/test/0005_DynamicProgramming/0007_DecodeWaysTest.cc b/test/0005_DynamicProgramming/0007_DecodeWaysTest.cc index e69de29..7dbcab8 100644 --- a/test/0005_DynamicProgramming/0007_DecodeWaysTest.cc +++ b/test/0005_DynamicProgramming/0007_DecodeWaysTest.cc @@ -0,0 +1,19 @@ +#include +#include "../../include/0005_DynamicProgramming/0007_DecodeWays.h" + +namespace DecodeWays +{ + TEST(DecodeWays, RecursionTest01) + { + // Arrange + DynamicProgramming dp; + string digits = "121"; + int expectedWaysCount = 3; + + // Act + int actualWaysCount = dp.RecursiveCountWays(digits); + + // Assert + ASSERT_EQ(expectedWaysCount, actualWaysCount); + } +} \ No newline at end of file From f54850535270fca93a6aabdd0fa8b2a13c12a484 Mon Sep 17 00:00:00 2001 From: Debashis Nandi Date: Sat, 18 Oct 2025 00:59:57 +0530 Subject: [PATCH 09/10] feature: decode way dp sol added --- .../0005_DynamicProgramming/0007_DecodeWays.h | 1 + .../0007_DecodeWays.cc | 26 +++++++++++++++++++ .../0007_DecodeWaysTest.cc | 14 ++++++++++ 3 files changed, 41 insertions(+) diff --git a/include/0005_DynamicProgramming/0007_DecodeWays.h b/include/0005_DynamicProgramming/0007_DecodeWays.h index a3591bb..2003209 100644 --- a/include/0005_DynamicProgramming/0007_DecodeWays.h +++ b/include/0005_DynamicProgramming/0007_DecodeWays.h @@ -27,5 +27,6 @@ namespace DecodeWays int CountWaysRecursiveHelper(string& digits, size_t index); public: int RecursiveCountWays(string digits); + int DpCountways(string digits); }; } \ No newline at end of file diff --git a/source/0005_DynamicProgramming/0007_DecodeWays.cc b/source/0005_DynamicProgramming/0007_DecodeWays.cc index a4c1561..180fd5f 100644 --- a/source/0005_DynamicProgramming/0007_DecodeWays.cc +++ b/source/0005_DynamicProgramming/0007_DecodeWays.cc @@ -32,4 +32,30 @@ namespace DecodeWays { return this->CountWaysRecursiveHelper(digits, 0); } + + int DynamicProgramming::DpCountways(string digits) + { + size_t digitsLength = digits.size(); + + vector dp(digitsLength + 1, 0); + + dp[digitsLength] = 1; + + for (int index = digitsLength - 1; index >= 0; index--) + { + // Single digit decoding: check if current digit is not '0'. + if (digits[index] != '0') + { + dp[index] = dp[index + 1]; + } + + // Two digit decoding: check if next two digits are valid. + if ((index + 1 < digitsLength) && ((digits[index] == '1' && digits[index + 1] <= '9') || (digits[index] == '2' && digits[index + 1] <= '6'))) + { + dp[index] += dp[index + 2]; + } + } + + return dp[0]; + } } \ No newline at end of file diff --git a/test/0005_DynamicProgramming/0007_DecodeWaysTest.cc b/test/0005_DynamicProgramming/0007_DecodeWaysTest.cc index 7dbcab8..0c0e516 100644 --- a/test/0005_DynamicProgramming/0007_DecodeWaysTest.cc +++ b/test/0005_DynamicProgramming/0007_DecodeWaysTest.cc @@ -16,4 +16,18 @@ namespace DecodeWays // Assert ASSERT_EQ(expectedWaysCount, actualWaysCount); } + + TEST(DecodeWays, DpTest01) + { + // Arrange + DynamicProgramming dp; + string digits = "121"; + int expectedWaysCount = 3; + + // Act + int actualWaysCount = dp.DpCountways(digits); + + // Assert + ASSERT_EQ(expectedWaysCount, actualWaysCount); + } } \ No newline at end of file From ae24b41ccc726cca4d0b71751fb4b59822aa9d49 Mon Sep 17 00:00:00 2001 From: Debashis Nandi Date: Sat, 18 Oct 2025 01:03:05 +0530 Subject: [PATCH 10/10] test: decode ways invalid case test added --- .../0005_DynamicProgramming/0007_DecodeWaysTest.cc | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/0005_DynamicProgramming/0007_DecodeWaysTest.cc b/test/0005_DynamicProgramming/0007_DecodeWaysTest.cc index 0c0e516..2196520 100644 --- a/test/0005_DynamicProgramming/0007_DecodeWaysTest.cc +++ b/test/0005_DynamicProgramming/0007_DecodeWaysTest.cc @@ -30,4 +30,18 @@ namespace DecodeWays // Assert ASSERT_EQ(expectedWaysCount, actualWaysCount); } + + TEST(DecodeWays, DpTestInvalidInput) + { + // Arrange + DynamicProgramming dp; + string digits = "230"; + int expectedWaysCount = 0; + + // Act + int actualWaysCount = dp.DpCountways(digits); + + // Assert + ASSERT_EQ(expectedWaysCount, actualWaysCount); + } } \ No newline at end of file