From 144b59d33114197ef0360fa527c3bc945f4bf6dd Mon Sep 17 00:00:00 2001 From: Megha Raykar Date: Mon, 6 Apr 2026 19:11:22 -0700 Subject: [PATCH 1/3] Problem1 added --- Problem1.py | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 Problem1.py diff --git a/Problem1.py b/Problem1.py new file mode 100644 index 00000000..7bdad220 --- /dev/null +++ b/Problem1.py @@ -0,0 +1,92 @@ +# https://leetcode.com/problems/coin-change/description/ + +# Solution 1 +# TC: 2^m+n +# SC: m+n +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No +# Explored the greedy logic and realized it doesn't fit. Then decided with exhaustive approach(recursive). This approach explores all +# the permutations and combinations. We have two options at every step, choose and not choose. Whenever the amount reaches +# 0, then it means we have a valid branch. We need to check for all the branches which has amount equals to 0, then compute +# the minimum of coinsUsed and return that. + +class Solution: + def coinChange(self, coins: List[int], amount: int) -> int: + self.minimum = float('inf') + self.helper(coins, amount, 0, 0) + if self.minimum == float('inf'): + return -1 + return self.minimum + + def helper(self, coins, amount, i, coinsUsed): + + if amount < 0 or i == len(coins): + return + + if amount == 0: + self.minimum = min(self.minimum, coinsUsed) + return + + # case 0 // not choose + self.helper(coins, amount, i+1, coinsUsed) + + # case 1 // choose + self.helper(coins, amount-coins[i], i, coinsUsed+1) + +# Solution 2 +# TC: O(m*n) +# SC: O(m*n) + +class Solution: + def coinChange(self, coins: List[int], amount: int) -> int: + + m = len(coins) + n = amount + + dp = [[0] * (n + 1) for _ in range(m+1)] + + for j in range(1, n+1): + dp[0][j] = 99999 + + for i in range(1, m+1): + for j in range(1, n+1): + if j < coins[i-1]: # no choose is available + dp[i][j] = dp[i-1][j] + else: + # min between choose and no choose + dp[i][j] = min(dp[i-1][j], 1 + dp[i][j- coins[i-1]]) + + if dp[m][n] == 99999: return -1 + + return dp[m][n] + + +# Solution 3 +# DP with tabulation using 1D array +# TC: O(m*n) +# SC: O(n) + +class Solution: + def coinChange(self, coins: List[int], amount: int) -> int: + + m = len(coins) + n = amount + + dp = [0] * (n + 1) + + for j in range(1, n+1): + dp[j] = 99999 + + for i in range(1, m+1): + for j in range(1, n+1): + if j < coins[i-1]: # no choose is available + dp[j] = dp[j] + else: + # min between choose and no choose + dp[j] = min(dp[j], 1 + dp[j- coins[i-1]]) + + if dp[n] == 99999: return -1 + + return dp[n] + + \ No newline at end of file From d9aee6506861d478d9552193d462f20a06025cb1 Mon Sep 17 00:00:00 2001 From: Megha Raykar Date: Mon, 6 Apr 2026 19:13:18 -0700 Subject: [PATCH 2/3] Comment added --- Problem1.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Problem1.py b/Problem1.py index 7bdad220..1c15ae49 100644 --- a/Problem1.py +++ b/Problem1.py @@ -34,6 +34,7 @@ def helper(self, coins, amount, i, coinsUsed): self.helper(coins, amount-coins[i], i, coinsUsed+1) # Solution 2 +# DP with tabulation using 2D matrix # TC: O(m*n) # SC: O(m*n) From e69175ff8619083ed48dee2ac7db3ae855fcf49c Mon Sep 17 00:00:00 2001 From: Megha Raykar Date: Thu, 9 Apr 2026 16:17:06 -0700 Subject: [PATCH 3/3] Problem2 added --- Problem2.py | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 Problem2.py diff --git a/Problem2.py b/Problem2.py new file mode 100644 index 00000000..5554bab5 --- /dev/null +++ b/Problem2.py @@ -0,0 +1,80 @@ +# https://leetcode.com/problems/house-robber/description/ + +# Exhaustive approach +# TC: O(2^n) +# SC: O(n) + +class Solution: + highest = 0 + def rob(self, nums: List[int]) -> int: + self.helper(nums, 0, 0) + return self.highest + + def helper(self, nums, i, robbings): + + if i >= len(nums): + self.highest = max(self.highest, robbings) + return + + # case 0 + self.helper(nums, i+1, robbings) + + # case 1 + self.helper(nums, i+2, robbings + nums[i]) + +# int based recursion + +class Solution: + highest = 0 + def rob(self, nums: List[int]) -> int: + return self.helper(nums, 0, 0) + + def helper(self, nums, i, robbings): + + if i >= len(nums): + return robbings + + # case 0 + case0 = self.helper(nums, i+1, robbings) + + # case 1 + case1 = self.helper(nums, i+2, robbings + nums[i]) + + return max(case0, case1) + +# DP solution +# TC: O(n) +# SC: O(n) + +class Solution: + def rob(self, nums: List[int]) -> int: + n = len(nums) + if n == 1: + return nums[0] + dp = [0] * n + + dp[0] = nums[0] + dp[1] = max(nums[0], nums[1]) + + for i in range(2, n): + dp[i] = max(dp[i-1], dp[i-2] + nums[i]) + + return dp[n-1] + +# Using curr and prev + +class Solution: + def rob(self, nums: List[int]) -> int: + n = len(nums) + if n == 1: + return nums[0] + + prev = nums[0] + curr = max(nums[0], nums[1]) + + for i in range(2, n): + temp = curr + curr = max(curr, nums[i] + prev) + prev = temp + + return curr \ No newline at end of file