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
56 changes: 56 additions & 0 deletions coin-change2.py
Original file line number Diff line number Diff line change
@@ -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]

42 changes: 42 additions & 0 deletions painthouse.py
Original file line number Diff line number Diff line change
@@ -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