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
37 changes: 37 additions & 0 deletions leetcode_256.py
Original file line number Diff line number Diff line change
@@ -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
42 changes: 42 additions & 0 deletions leetcode_518.py
Original file line number Diff line number Diff line change
@@ -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]