From f558c47bb13165d77772b9adf8f5c3c20289ed2f Mon Sep 17 00:00:00 2001 From: Anneshu Nag <132702983+NK-Works@users.noreply.github.com> Date: Mon, 14 Oct 2024 10:59:06 +0530 Subject: [PATCH] dp-basic-series --- .../Basic-DP-Problems/climbing-stairs.py | 21 +++++++++++++++++ .../Basic-DP-Problems/fibonacci-seq.py | 20 ++++++++++++++++ .../Basic-DP-Problems/house-robber.py | 23 +++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/climbing-stairs.py create mode 100644 Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/fibonacci-seq.py create mode 100644 Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/house-robber.py diff --git a/Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/climbing-stairs.py b/Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/climbing-stairs.py new file mode 100644 index 0000000000..338e84ec7f --- /dev/null +++ b/Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/climbing-stairs.py @@ -0,0 +1,21 @@ +def climbStairs(n): + # Handle base cases + if n == 1: + return 1 + if n == 2: + return 2 + + # Create a table to store the number of ways to reach each step + dp = [0] * (n + 1) + dp[1] = 1 # 1 way to reach the first step + dp[2] = 2 # 2 ways to reach the second step + + # Fill the table in a bottom-up manner + for i in range(3, n + 1): + dp[i] = dp[i - 1] + dp[i - 2] + + return dp[n] + +# Test the function +n = 5 +print(f"Number of ways to climb {n} stairs: {climbStairs(n)}") diff --git a/Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/fibonacci-seq.py b/Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/fibonacci-seq.py new file mode 100644 index 0000000000..25fbcb0614 --- /dev/null +++ b/Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/fibonacci-seq.py @@ -0,0 +1,20 @@ +def fibonacci(n): + # Handle base cases + if n == 0: + return 0 + if n == 1: + return 1 + + # Create a table to store Fibonacci numbers + fib = [0] * (n + 1) + fib[1] = 1 + + # Fill the table in a bottom-up manner + for i in range(2, n + 1): + fib[i] = fib[i - 1] + fib[i - 2] + + return fib[n] + +# Test the function +n = 10 +print(f"Fibonacci number at position {n} is: {fibonacci(n)}") diff --git a/Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/house-robber.py b/Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/house-robber.py new file mode 100644 index 0000000000..1a7f5f02d3 --- /dev/null +++ b/Algorithms_and_Data_Structures/Dynamic-Programming-Series/Basic-DP-Problems/house-robber.py @@ -0,0 +1,23 @@ +def rob(houses): + # If there are no houses, return 0 + if not houses: + return 0 + # If there is only one house, return its value + if len(houses) == 1: + return houses[0] + + # Create a table to store the maximum money that can be robbed up to each house + dp = [0] * len(houses) + dp[0] = houses[0] # Max money for the first house + dp[1] = max(houses[0], houses[1]) # Max money for the first two houses + + # Fill the table in a bottom-up manner + for i in range(2, len(houses)): + # For each house, decide whether to rob it (dp[i-2] + houses[i]) or not (dp[i-1]) + dp[i] = max(dp[i - 1], dp[i - 2] + houses[i]) + + return dp[-1] + +# Test the function +houses = [2, 7, 9, 3, 1] +print(f"Maximum money that can be robbed: {rob(houses)}")