diff --git a/leetcode_256.py b/leetcode_256.py new file mode 100644 index 00000000..2e16e9fa --- /dev/null +++ b/leetcode_256.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Created on Sun Feb 22 13:19:10 2026 + +@author: rishigoswamy + + Problem: + Each house can be painted Red, Green, or Blue. + No two adjacent houses can have the same color. + Return the minimum total cost. + + Approach: + Dynamic Programming. + For each house: + cost[color] = current cost + + minimum of previous house's other two colors + + Time Complexity: O(n) + Space Complexity: O(1) + +""" + +from typing import List + +#https://leetcode.com/problems/paint-house/ +class Solution: + def minCost(self, costs: List[List[int]]) -> int: + currCost = costs[0] + val = min(costs[0]) + for i in range(1, len(costs)): + costR = costs[i][0] + min(currCost[1], currCost[2]) + costG = costs[i][1] + min(currCost[0], currCost[2]) + costB = costs[i][2] + min(currCost[1], currCost[0]) + val = min(costR, costG, costB) + currCost = [costR, costG, costB] + return val \ No newline at end of file diff --git a/leetcode_518.py b/leetcode_518.py new file mode 100644 index 00000000..60e3c21a --- /dev/null +++ b/leetcode_518.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Created on Sun Feb 22 13:21:36 2026 + +@author: rishigoswamy + + + Problem: + #https://leetcode.com/problems/coin-change-ii/description/ + + Count number of combinations to make 'amount' + using unlimited supply of given coins. + + Approach: + 2D Dynamic Programming + + dp[i][j] = + number of ways to make amount j + using first i coins + + Time Complexity: O(n * amount) + Space Complexity: O(n * amount) + +""" + + +from typing import List +class Solution: + def change(self, amount: int, coins: List[int]) -> int: + dp = [[0] * (amount + 1) for _ in range(len(coins)+1)] + + for i in range(0, len(dp)): + dp[i][0] = 1 + + for i in range(1,len(dp)): + for j in range(len(dp[0])): + currCoin = coins[i-1] + dp[i][j] = dp[i-1][j] + if j-currCoin >= 0: + dp[i][j] += dp[i][j-currCoin] + return dp[-1][-1]