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
26 changes: 26 additions & 0 deletions coin-change.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
''' Time Complexity : O(m * n)
Space Complexity : O(m * n)
Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this : No

Your code here along with comments explaining your approach

Approach : calculating min no of coins required at each amount
'''
class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if amount == 0:
return 0
dp = [[amount+1 for _ in range(amount+1)] for _ in range(len(coins)+1)]
for i in range(len(coins)+1):
dp[i][0] = 0
for i in range(1,len(coins)+1):
for j in range(1,(amount+1)):
if coins[i-1] > j:
dp[i][j] = dp[i-1][j]
else:
dp[i][j] = min(dp[i-1][j], dp[i][j-coins[i-1]] + 1)
if dp[i][j] == (amount+1):
return -1
else:
return dp[i][j]
19 changes: 19 additions & 0 deletions house-robber.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
''' Time Complexity : O(n)
Space Complexity : O(n)
Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this : No

Your code here along with comments explaining your approach
'''

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