From 3628765f439dcd8667c11bf10c3994afd5785fee Mon Sep 17 00:00:00 2001 From: "M.Hitesh" <54840773+hiteshmadapathi@users.noreply.github.com> Date: Sat, 1 Jan 2022 05:17:18 -0500 Subject: [PATCH 1/5] Add files via upload --- coin change.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 coin change.py diff --git a/coin change.py b/coin change.py new file mode 100644 index 00000000..bd81148f --- /dev/null +++ b/coin change.py @@ -0,0 +1,24 @@ +# Time Complexity: O(mn) +# Space Complexity: O(mn) + +class Solution: + + def coinChange(self, coins: List[int], amount: int) -> int: + + dp = [[0 for j in range(amount+1)] for i in range(len(coins)+1)] # creating a matrix + for x in range(len(coins) + 1): + dp[x][0] = 0 + for k in range(1,len(dp[0])): + dp[0][k] = amount+1 # initializing the values in 1st row to infinity + for i in range(1,len(dp)): + for j in range(1,len(dp[0])): + # coin value greater than denomination then provide same values as in previous row + if j < coins[i-1]: + dp[i][j] = dp[i-1][j] + else: + dp[i][j] = min(dp[i-1][j], dp[i][j-coins[i-1]]+1) + result = dp[len(dp)-1][len(dp[0])-1] + if result > amount: + return -1 + else: + return result \ No newline at end of file From f3fa7e889f55091365383f303d0a632afaeb285f Mon Sep 17 00:00:00 2001 From: "M.Hitesh" <54840773+hiteshmadapathi@users.noreply.github.com> Date: Wed, 5 Jan 2022 15:30:44 -0500 Subject: [PATCH 2/5] Create house_robber.py --- house_robber.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 house_robber.py diff --git a/house_robber.py b/house_robber.py new file mode 100644 index 00000000..aa853437 --- /dev/null +++ b/house_robber.py @@ -0,0 +1,23 @@ +# Time Complexity - O(n) +# Space Complexity - O(n) + +class Solution: + + def rob(self, nums: List[int]) -> int: + dp = [[0 for j in range(2)] for i in range(len(nums))] + dp[0][0] = 0 + dp[0][1] = nums[0] + for i in range(1,len(dp)): + # not choosing + dp[i][0] = max(dp[i-1][0],dp[i-1][1]) + # choosing + dp[i][1] = dp[i-1][0]+nums[i] + result = max(dp[-1][0],dp[-1][1]) + + # houses robbed to get loot maximum money + houses = [] + for i in range(len(dp)-1,-1,-1): + if dp[i][1]==result: + houses.append(nums[i]) + result = result-nums[i] + return sum(houses) From 0c0f2e62a16a7c18bd50a35d5dd843cf7eee7672 Mon Sep 17 00:00:00 2001 From: "M.Hitesh" <54840773+hiteshmadapathi@users.noreply.github.com> Date: Thu, 16 Apr 2026 06:44:37 -0700 Subject: [PATCH 3/5] Delete coin change.py --- coin change.py | 24 ------------------------ 1 file changed, 24 deletions(-) delete mode 100644 coin change.py diff --git a/coin change.py b/coin change.py deleted file mode 100644 index bd81148f..00000000 --- a/coin change.py +++ /dev/null @@ -1,24 +0,0 @@ -# Time Complexity: O(mn) -# Space Complexity: O(mn) - -class Solution: - - def coinChange(self, coins: List[int], amount: int) -> int: - - dp = [[0 for j in range(amount+1)] for i in range(len(coins)+1)] # creating a matrix - for x in range(len(coins) + 1): - dp[x][0] = 0 - for k in range(1,len(dp[0])): - dp[0][k] = amount+1 # initializing the values in 1st row to infinity - for i in range(1,len(dp)): - for j in range(1,len(dp[0])): - # coin value greater than denomination then provide same values as in previous row - if j < coins[i-1]: - dp[i][j] = dp[i-1][j] - else: - dp[i][j] = min(dp[i-1][j], dp[i][j-coins[i-1]]+1) - result = dp[len(dp)-1][len(dp[0])-1] - if result > amount: - return -1 - else: - return result \ No newline at end of file From 4bb2a5b0af2958034955494a11dadf03bf43ee7e Mon Sep 17 00:00:00 2001 From: "M.Hitesh" <54840773+hiteshmadapathi@users.noreply.github.com> Date: Thu, 16 Apr 2026 06:44:47 -0700 Subject: [PATCH 4/5] Delete house_robber.py --- house_robber.py | 23 ----------------------- 1 file changed, 23 deletions(-) delete mode 100644 house_robber.py diff --git a/house_robber.py b/house_robber.py deleted file mode 100644 index aa853437..00000000 --- a/house_robber.py +++ /dev/null @@ -1,23 +0,0 @@ -# Time Complexity - O(n) -# Space Complexity - O(n) - -class Solution: - - def rob(self, nums: List[int]) -> int: - dp = [[0 for j in range(2)] for i in range(len(nums))] - dp[0][0] = 0 - dp[0][1] = nums[0] - for i in range(1,len(dp)): - # not choosing - dp[i][0] = max(dp[i-1][0],dp[i-1][1]) - # choosing - dp[i][1] = dp[i-1][0]+nums[i] - result = max(dp[-1][0],dp[-1][1]) - - # houses robbed to get loot maximum money - houses = [] - for i in range(len(dp)-1,-1,-1): - if dp[i][1]==result: - houses.append(nums[i]) - result = result-nums[i] - return sum(houses) From feee21dea0d70fa85f228b3bbf02a1cb4a28f6b7 Mon Sep 17 00:00:00 2001 From: "M.Hitesh" <54840773+hiteshmadapathi@users.noreply.github.com> Date: Thu, 16 Apr 2026 20:59:22 -0700 Subject: [PATCH 5/5] Create Problem1.py --- Problem1.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Problem1.py diff --git a/Problem1.py b/Problem1.py new file mode 100644 index 00000000..8d5feffa --- /dev/null +++ b/Problem1.py @@ -0,0 +1,19 @@ +# Time Complexity --> O(m*n) where m is the amount and n is the number of coins +# Space Complexity --> O(m) +# Approach --> Still going over all the possibilities but optimizing the search by reusing the result for repeated subproblems. This is the Tabulation DP, a bottom up approach. The space complexity is optimized to use only a single array of length m+1 since the result at each node only depends on its curent value or the value before that position in array. +class Solution: + def coinChange(self, coins: List[int], amount: int) -> int: + n = len(coins) + m = amount + dp = [0 for i in range(m+1)] + for i in range(1,m+1): + dp[i] = amount+1 + + for i in range(1,n+1): + for j in range(1,m+1): + if j>=coins[i-1]: + dp[j] = min(dp[j], 1+dp[j-coins[i-1]]) + #print(dp) + if dp[m]>amount: + return -1 + return dp[m]