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
46 changes: 46 additions & 0 deletions problem-1.py
Original file line number Diff line number Diff line change
@@ -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))
44 changes: 44 additions & 0 deletions problem-2.py
Original file line number Diff line number Diff line change
@@ -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<coins[i-1]):
# dp[i][j]=dp[i-1][j]
# else:
# dp[i][j]=dp[i-1][j]+dp[i][j-coins[i-1]]
# return dp[m][n]

#Approach 2
# The column is updated as per the coin value that we are iterating and on that
# basis we get a final answer which will be the last column of the given matrix
m=len(coins)
n=amount
dp=[0]*(n+1)
dp[0]=1
for i in range(1,m+1):
for j in range(coins[i-1],n+1):
dp[j]=dp[j]+dp[j-coins[i-1]]
return dp[n]