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
21 changes: 21 additions & 0 deletions CoinChange.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# 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 the min coins to make amount i.
# For each coin, update the array by either using coin or skipping it.
# At the end, if dp[n] is still inf, it means amount can’t be formed.

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

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

if dp[amount] == float('inf'):
return -1

return dp[amount]
21 changes: 21 additions & 0 deletions HouseRobber.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Time Complexity : O(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 the max amount robbed.
# For each house, update the array by either robbing it or skipping it.

class Solution:
def rob(self, nums: List[int]) -> int:
if not nums:
return 0

n = len(nums)
maxRobbedAmt = [None] * (n + 1)

maxRobbedAmt[n], maxRobbedAmt[n-1] = 0, nums[n-1]

for i in range(n-2, -1, -1):
maxRobbedAmt[i] = max(maxRobbedAmt[i+1], maxRobbedAmt[i+2] + nums[i])

return maxRobbedAmt[0]