From b67dfbb25cfdcca61f4c65598a44cf4e1474199e Mon Sep 17 00:00:00 2001 From: Kattuvila Milkiyas Date: Tue, 25 Nov 2025 00:30:33 -0800 Subject: [PATCH] dp-2 completed --- coin-change2.py | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ painthouse.py | 42 +++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 coin-change2.py create mode 100644 painthouse.py diff --git a/coin-change2.py b/coin-change2.py new file mode 100644 index 00000000..e7e36815 --- /dev/null +++ b/coin-change2.py @@ -0,0 +1,56 @@ +Initialize a 2x2 array with rows = length of coins+1 and cols amount +1. Make the first row of +the array as 0 and the first column as 1. FOr each denomination of coin and each amount from 0 to +amount,, check if the denomination is grater than the amount, if it is, the value of dp for that +row and colum will be the same as the previous row else, it will be the summation of the previous row +and the current row and colum-denomination block away. + +Time complexity O(m*n), m is amount, n is numver of denomination of coins +Space complexity O(m*n) m is amount, n is numver of denomination of coins + + + +class Solution(object): + def change(self, amount, coins): + """ + :type amount: int + :type coins: List[int] + :rtype: int + """ + rows = len(coins)+1 + cols = amount+1 + + dp = [[None for i in range(cols)]for i in range(rows)] + for i in range(cols): + dp[0][i]=0 + for i in range(rows): + dp[i][0]=1 + for i,c in enumerate(coins): + for j in range(1,cols): + if c>j: + dp[i+1][j]=dp[i][j] + else: + dp[i+1][j]=dp[i][j]+dp[i+1][j-c] + return dp[rows-1][cols-1] + + + + +# class Solution(object): +# def change(self, amount, coins): +# """ +# :type amount: int +# :type coins: List[int] +# :rtype: int +# """ +# rows = len(coins)+1 +# cols = amount+1 + +# dp = [0 for i in range(cols)] + +# dp[0]=1 +# for i,c in enumerate(coins): +# for j in range(1,cols): +# if c<=j: +# dp[j]=dp[j]+dp[j-c] +# return dp[cols-1] + \ No newline at end of file diff --git a/painthouse.py b/painthouse.py new file mode 100644 index 00000000..5c371470 --- /dev/null +++ b/painthouse.py @@ -0,0 +1,42 @@ +# Initialize a 2x2 array with rows of the number of houses and columns with the number of colors. +# For each house/row, assign the value of each color as a summation of its current value and the minimum +# of the other 2 colors. Return the the min of last row + +# Time complexity: O(n) +# Space complexity: O(n) + +class Solution(object): + def minCost(self, costs): + """ + :type costs: List[List[int]] + :rtype: int + """ + # edge case + if not costs: + return 0 + + dp= [row[:] for row in costs] + rows = len(costs) + cols = len(costs[0]) + + + print(dp) + for i in range(1,rows): + dp[i][0]=dp[i][0]+min(dp[i-1][1],dp[i-1][2]) + dp[i][1]=dp[i][1]+min(dp[i-1][0],dp[i-1][2]) + dp[i][2]=dp[i][2]+min(dp[i-1][1],dp[i-1][0]) + + return min(dp[rows-1]) +costs = [ + [17, 2, 17], + [16, 16, 5], + [14, 3, 19] +] + +sol = Solution() +print(sol.minCost(costs)) + # dp initialization (first house same as cost) + + # iterate through houses + + # return final minimum