diff --git a/coin_change_2.py b/coin_change_2.py new file mode 100644 index 00000000..6036ca64 --- /dev/null +++ b/coin_change_2.py @@ -0,0 +1,16 @@ +from typing import List + +class Solution: + def change(self, amount: int, coins: List[int]) -> int: + dp = [0] * (amount + 1) + + dp[0] = 1 + + for coin in coins: + for curr_amount in range(coin, amount + 1): + dp[curr_amount] += dp[curr_amount - coin] + + return dp[amount] + +# Time Complexity: O(amount * n), where n is the number of coins +# Space Complexity: O(amount) \ No newline at end of file diff --git a/paint_house.py b/paint_house.py new file mode 100644 index 00000000..c7b67c45 --- /dev/null +++ b/paint_house.py @@ -0,0 +1,18 @@ +class Solution: + def minCost(self, costs: List[List[int]]) -> int: + if not costs: + return 0 + + red, blue, green = costs[0] + + for i in range(1, len(costs)): + new_red = costs[i][0] + min(blue, green) + new_blue = costs[i][1] + min(red, green) + new_green = costs[i][2] + min(red, blue) + + red, blue, green = new_red, new_blue, new_green + + return min(red, blue, green) + + # Time Complexity: O(n), where n is the number of houses + # Space Complexity: O(1) \ No newline at end of file