diff --git a/coin-change-2.py b/coin-change-2.py new file mode 100644 index 00000000..347b1530 --- /dev/null +++ b/coin-change-2.py @@ -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] diff --git a/paint-house.py b/paint-house.py new file mode 100644 index 00000000..0b0c5e06 --- /dev/null +++ b/paint-house.py @@ -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]) \ No newline at end of file