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
44 changes: 44 additions & 0 deletions problem1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
# memo={}
# def helper(index,total):
# if (index,total) in memo:
# return memo[(index,total)]
# if total==amount:
# return 0
# if index>=len(coins) or total>amount:
# return float('inf')

# take = 1+ helper(index,total+coins[index])
# skip = helper(index+1, total)
# memo[(index,total)] = min(take,skip)
# return memo[(index,total)]

# result = helper(0,0)
# return result if result!=float('inf') else -1


# Inititlie the dp array with 0's with one extra row and one extra column
dp = [[0]*(amount+1) for i in range(len(coins)+1)]

# Initialize the first row with max value as we can not make up those amount with 0 coin
for i in range(1,amount+1):
dp[0][i]=float('inf')

# Starting iteration for the 1st coin
for c in range(1,len(coins)+1):
for amt in range(1,amount+1):
# If we can make that amount with the given coin
if amt-coins[c-1]>=0:
# It has to be the minimum of not chosing that coin which is present above
# and chosing the coin, if we chose that see the value in the sub problem with the amount-coin
dp[c][amt] = min(dp[c-1][amt],1+dp[c][amt-coins[c-1]])
else:
#chose the row above
dp[c][amt]=dp[c-1][amt]
# Check the last row and column of our dp table as this is where we have all the coins available and the amount
if dp[len(coins)][amount]!=float('inf'):
return dp[len(coins)][amount]
else:
return -1

24 changes: 24 additions & 0 deletions problem2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution:
def rob(self, nums: List[int]) -> int:
# memo={}
# def helper(index,total):
# if (index,total) in memo:
# return memo[(index,total)]
# if index>=len(nums):
# return total
# rob= helper(index+2,total+nums[index])
# skip= helper(index+1, total)
# memo[(index,total)]= max(rob,skip)
# return memo[(index,total)]
# return helper(0,0)

n=len(nums)
# Taking an extra 0 for the ease of calculation
dp = [0]*(n+1)
dp[1]=nums[0]
for i in range(2,n+1):
# Either chose the current house and add it to the total of previous houses u robbed until now
# or dont rob so the total would be max until the prev house
dp[i]=max(dp[i-1],dp[i-2]+nums[i-1])
return dp[n]