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 Problem1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'''
The problem is same as house robber. We just have 3 choices to make.
Time complexity: O(n)
Space complexity: O(1)
'''

class Solution:
def paint(self, costs: List[int][int]) -> int:
dp = [math.inf, math.inf, math.inf]

for cost in costs:
chooseRed = cost[0] + min(dp[2], dp[1])
chooseGreen = cost[1] + min(dp[0], dp[2])
chooseBlue = cost[2] + min(dp[0], dp[1])

dp = [chooseRed, chooseGreen, chooseBlue]

return max(dp)
38 changes: 38 additions & 0 deletions Problem2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'''
The brute force is to make a tree of choosing a coin or not and keep traversing
until the amount is reached. The branches that reach the amount are the total
number of ways to reach the amount.
Space complexity: O(amount/min(coins))
Time complexity: O(2^(amount/min(coins)))

This produces repeated nodes and hence dp can be used.

The number of ways to reach 3 using coins[1,2] is
1. 1+1+1
2. 1+2

Which can be also seen as:
1. Number of ways to reach 3 using 1s
2. Number of ways to reach 3 using 2s and 1s.
- This can be further broken down into:
1) Number of ways to reach 1 using 1s and 2s and then using a coin of 2
This keeps the number of ways to reach 3 same as number of ways to reach 1.

We formulate this into a table and iterate over the whole table to find the final number
of ways to reach the amount using all the given coins.

Time complexity: O(amount * len(coins))
Space complexity: O(amount * len(coins))
'''

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

for coin in coins:
for total in range(amount+1):
if total >= coin:
dp[total] = dp[total] + dp[total-coin]

return dp[-1]