Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions coin-change-2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#-------------Solution 1 : 2D Matrix--------
''' Time Complexity : O(m*n)
Space Complexity : O(m*n)

Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this : No
'''

class Solution:
def change(self, amount: int, coins: List[int]) -> int:
n = len(coins)
dp = [[0 for _ in range(amount+1)] for _ in range(n+1)]
dp[0][0] = 1
for i in range(1,n+1):
for j in range(amount+1):
coin = coins[i-1]
if coin > j:
dp[i][j] = dp[i-1][j]
else:
dp[i][j] = dp[i-1][j] + dp[i][j-coin]
return dp[n][amount]


#-------------Solution 2 : 1D Array--------
''' Time Complexity : O(m*n)
Space Complexity : O(m)

Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this : No
'''

class Solution:
def change(self, amount: int, coins: List[int]) -> int:
n = len(coins)
dp = [0 for _ in range(amount+1)]
dp[0] = 1
for i in range(0,n):
for j in range(amount+1):
coin = coins[i]
if coin <= j:
dp[j] = dp[j] + dp[j-coin]
return dp[amount]
24 changes: 24 additions & 0 deletions paint-house.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#-------------Solution 1 : 2D Matrix--------
''' Time Complexity : O(m*n)
Space Complexity : O(m*n)

Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this : No
'''
class Solution:
def minCost(self, costs: List[List[int]]) -> int:
rows= len(costs)
cols = len(costs[0])
dp = [[0 for _ in range(cols)] for _ in range(rows)]
for i in range(rows):
for j in range(cols):
if i == 0 :
dp[i][j] = costs[i][j]
else:
if j == 0:
dp[i][j] = min(costs[i][j]+dp[i-1][1], costs[i][j]+dp[i-1][2])
if j == 1:
dp[i][j] = min(costs[i][j]+dp[i-1][0], costs[i][j]+dp[i-1][2])
if j == 2:
dp[i][j] = min(costs[i][j]+dp[i-1][0], costs[i][j]+dp[i-1][1])
return min(dp[rows-1][0],dp[rows-1][1],dp[rows-1][2])