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
33 changes: 33 additions & 0 deletions CoinChange2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Solution(object):
def change(self, amount, coins):
"""
:type amount: int
:type coins: List[int]
:rtype: int
"""
if amount == 0:
return 1

#Let us create a DP array.
dp = []
for i in range(len(coins) + 1):
dp.append([0] * (amount+1)) #The reason why we intialize with 0 is worst case, we wont be able to
# make the amount.
dp[i][0] = 1

for i in range(1,len(dp)):
for j in range(1, len(dp[0])):
# If the amount is less that coin being considered, we will skip
# choosing the coin. This is by basically using the value exactly
# above as it is the value that we have in hand for generating the
# same amount without using the current coin.
if j < coins[i-1]:
dp[i][j] = dp[i-1][j]

else:
#Given that we are calculating the total amount, we can simply
#calculate the total amount generated by using the coin and not
#using it.
dp[i][j] = dp[i-1][j] + dp[i][j-coins[i-1]] #dp[i][j-coins[i-1]] <-- remaining amount using the same coin.
#print(dp)
return dp[-1][-1]
25 changes: 25 additions & 0 deletions PaintHouse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution(object):
def minCost(self, costs):
"""
:type costs: List[List[int]]
:rtype: int
"""
if not costs:
return 0

dp = []
for i in range(len(costs)):
if i == 0:
dp.append(costs[0])
else:
#if i == dp.length, add a new array
if i == len(dp):
dp.append([0, 0, 0])

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[-1])

#TC : O(n)
#SC : O(n)