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
18 changes: 18 additions & 0 deletions CoinChange2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Time Complexity : O(m*n)
# Space Complexity : O(n)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No
# Approach : dp[i] is number of ways to make amount i.
# dp[i] = dp[i - coin]

class Solution:
def change(self, amount: int, coins: List[int]) -> int:
dp = [0] * (amount + 1)
dp[0] = 1

for coin in coins:
for a in range(coin, amount + 1):
dp[a] += dp[a-coin]

return dp[amount]

27 changes: 27 additions & 0 deletions PaintHouse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Time Complexity : O(n)
# Space Complexity : O(1)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No
# Approach : We work backwards from the last house.
# For each house, update the color costs based on the min of the other two colors from the next house.

class Solution:
def minCost(self, costs: List[List[int]]) -> int:
num_houses = len(costs)
num_colors = len(costs[0])

costR = costs[num_houses-1][0]
costB = costs[num_houses-1][1]
costG = costs[num_houses-1][2]


for i in range(num_houses - 2, -1, -1):
tempR = costR
tempB = costB

costR = costs[i][0] + min(costB, costG)
costB = costs[i][1] + min(tempR, costG)
costG = costs[i][2] + min(tempR, tempB)

return min(costR, costB, costG)