From 7de16b944a0e1c1b0c23331e692c3d471c580805 Mon Sep 17 00:00:00 2001 From: pratikb0501 Date: Sat, 18 Apr 2026 16:50:48 -0700 Subject: [PATCH] Solved DP-1 --- Problem_1.py | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Problem_2.py | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 144 insertions(+) create mode 100644 Problem_1.py create mode 100644 Problem_2.py diff --git a/Problem_1.py b/Problem_1.py new file mode 100644 index 00000000..a8eecc7e --- /dev/null +++ b/Problem_1.py @@ -0,0 +1,73 @@ +## Problem1 (https://leetcode.com/problems/coin-change/) + + +# Brute Force + +''' +class Solution: + def coinChange(self, coins: List[int], amount: int) -> int: + result = self.getCoins(0, amount, coins) + if result == 1e9: + return -1 + return result + + def getCoins(self, index, amount, coins): + if index >= len(coins) or amount < 0: + return 1e9 + if amount == 0: + return 0 + not_choose = self.getCoins(index + 1, amount, coins) + choose = 1 + self.getCoins(index, amount - coins[index], coins) + return min(not_choose, choose) + +''' + +# DP using memoization (Top Down) +''' +class Solution: + def coinChange(self, coins: List[int], amount: int) -> int: + n = len(coins) + memo = [[-1] * (amount + 1) for _ in range(n)] + result = self.getCoins(0, amount, coins, memo) + if result == 1e9: + return -1 + return result + + def getCoins(self, index, amount, coins, memo): + if amount == 0: + return 0 + if index >= len(coins) or amount < 0: + return 1e9 + if memo[index][amount] != -1: + return memo[index][amount] + not_choose = self.getCoins(index + 1, amount, coins, memo) + choose = 1 + self.getCoins(index, amount - coins[index], coins, memo) + memo[index][amount] = min(not_choose, choose) + return memo[index][amount] + +''' + +# Tabulation + +class Solution: + def coinChange(self, coins: List[int], amount: int) -> int: + n = len(coins) + INF = int(1e9) + + memo = [[0] * (amount + 1) for _ in range(n + 1)] + + for target in range(1, amount + 1): + memo[n][target] = INF + + for index in range(n - 1, -1, -1): + for target in range(amount + 1): + not_choose = memo[index + 1][target] + + choose = INF + if target >= coins[index]: + choose = 1 + memo[index][target - coins[index]] + + memo[index][target] = min(not_choose, choose) + + result = memo[0][amount] + return -1 if result == INF else result diff --git a/Problem_2.py b/Problem_2.py new file mode 100644 index 00000000..fa150a5b --- /dev/null +++ b/Problem_2.py @@ -0,0 +1,71 @@ +## Problem2 (https://leetcode.com/problems/house-robber/) + +# Brute Force + +''' + +class Solution: + def rob(self, nums: List[int]) -> int: + return self.getMax(0, nums) + + def getMax(self, index, nums): + if index >= len(nums): + return 0 + not_robbed = self.getMax(index + 1, nums) + robbed = nums[index] + self.getMax(index + 2, nums) + return max(not_robbed, robbed) + +''' + +# Memoization (Top Down) +''' +class Solution: + def rob(self, nums: List[int]) -> int: + n = len(nums) + memo = [-1] * n + return self.getMax(0, nums, memo) + + def getMax(self, index, nums, memo): + if index >= len(nums): + return 0 + if memo[index] != -1: + return memo[index] + not_robbed = self.getMax(index + 1, nums, memo) + robbed = nums[index] + self.getMax(index + 2, nums, memo) + memo[index] = max(not_robbed, robbed) + return memo[index] + +''' + +# Tabulation (bottom up) + +''' +class Solution: + def rob(self, nums: List[int]) -> int: + n = len(nums) + memo = [0] * (n + 2) + for index in range(n - 1, -1, -1): + not_robbed = memo[index + 1] + robbed = nums[index] + memo[index + 2] + memo[index] = max(not_robbed, robbed) + return memo[0] +''' + +# Space Optimised Tabulation + +class Solution: + def rob(self, nums): + n = len(nums) + memo_plus_1, memo_plus_2 = 0, 0 + result = 0 + for index in range(n - 1, -1, -1): + not_robbed = memo_plus_1 + robbed = nums[index] + memo_plus_2 + result = max(not_robbed, robbed) + memo_plus_2 = memo_plus_1 + memo_plus_1 = result + return result + +nums = [2,7,9,3,1] +sl = Solution() +print(sl.rob(nums)) \ No newline at end of file