From 88fd5b4347096af71e39aba618afb4bba2ab3cec Mon Sep 17 00:00:00 2001 From: RITIKA CHAUBE Date: Wed, 17 Jun 2026 19:33:46 -0400 Subject: [PATCH] Done DP-2 --- problem-1.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ problem-2.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 problem-1.py create mode 100644 problem-2.py diff --git a/problem-1.py b/problem-1.py new file mode 100644 index 00000000..2d8ef499 --- /dev/null +++ b/problem-1.py @@ -0,0 +1,46 @@ +# LEETCODE PROBLEM PAINT HOUSE +# TIME COMPLEXITY:O(N) where N is the array length for the given number of colors present +# SPACE COMPLEXITY: O(1) +# Any problem you faced while coding this: None + + +class Solution: + def minCost(self,costs): + + #Approach 1 + # m=len(costs) + # n=len(costs[0]) + + # dp=[[0]*n for _ in range(m)] + + # dp[0][0]=costs[0][0] + # dp[0][1]=costs[0][1] + # dp[0][2]=costs[0][2] + + # for i in range(1,m): + # dp[i][0]=costs[i][0]+min(dp[i-1][1],dp[i-1][2]) + # dp[i][1]=costs[i][1]+min(dp[i-1][0],dp[i-1][2]) + # dp[i][2]=costs[i][2]+min(dp[i-1][0],dp[i-1][1]) + + # return min(dp[m-1][0],dp[m-1][1],dp[m-1][2]) + + #Approach 2 + m=len(costs) + n=costs[0] + colorRed=costs[0][0] + colorBlue=costs[0][1] + colorGreen=costs[0][2] + + for i in range(1,m): + tempRed=colorRed + tempBlue=colorBlue + + colorRed=costs[i][0]+min(colorBlue,colorGreen) + colorBlue=costs[i][1]+min(tempRed,colorGreen) + colorGreen=costs[i][2]+min(tempRed,tempBlue) + + return min(colorRed,colorBlue,colorGreen) + +costs=[[17,2,17],[16,16,5],[14,3,19]] +sol=Solution() +print(sol.minCost(costs)) \ No newline at end of file diff --git a/problem-2.py b/problem-2.py new file mode 100644 index 00000000..7a096578 --- /dev/null +++ b/problem-2.py @@ -0,0 +1,44 @@ +# LEETCODE PROBLEM 518. COIN CHANGE II +# TIME COMPLEXITY:O(M *N) where M is the number of coins and N is the amount +# SPACE COMPLEXITY: O(N) where N amount +# Any problem you faced while coding this: Had a few hiccup in like understanding also while +# writing it + + + +class Solution(object): + def change(self, amount, coins): + """ + :type amount: int + :type coins: List[int] + :rtype: int + """ + #Approach 1 + # In this method a table is being made and the referencing is being done on the + # basis of the value of j example if j is 6 and is greater than the coin we are + # referencing ie 5 we would add the value present in the row above the same + # col and in the current row we will go back to 6-5=1 column to get value and then + # add it + # m=len(coins) + # n=amount + # dp=[[0]*(n+1) for _ in range(m+1)] + # dp[0][0]=1 + # for i in range(1,m+1): + # for j in range(n+1): + # if (j