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
19 changes: 19 additions & 0 deletions CoinChange.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#Time Complexity: O(n*m) where n is the amount and m is the number of coins
#Space Complexity: O(n) where n is the amount

class Solution(object):
def coinChange(self, coins, amount):
"""
:type coins: List[int]
:type amount: int
:rtype: int
"""
n = amount
dp = [99999] * (n + 1)
dp[0] = 0

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

return -1 if dp[n] == 99999 else dp[n]
31 changes: 31 additions & 0 deletions HouseRobber.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#Time Complexity: O(n) where n is the length of the input list
#Space Complexity: O(1) since we are using constant space

class Solution(object):
def rob(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
"""n = len(nums)
if n == 1:
return nums[0]
dp = [0] * n
dp[0] = nums[0]
dp[1] = max(nums[0], nums[1])
for i in range(2, len(nums)):
dp[i] = max(dp[i - 1], dp[i - 2] + nums[i])

return dp[-1]"""
n = len(nums)
if n == 1:
return nums[0]
prev = nums[0]
curr = max(nums[0], nums[1])
for i in range(2, len(nums)):
temp = curr
curr = max(curr, prev + nums[i])
prev = temp

return curr