diff --git a/problem1.py b/problem1.py new file mode 100644 index 00000000..6e02b431 --- /dev/null +++ b/problem1.py @@ -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 + \ No newline at end of file diff --git a/problem2.py b/problem2.py new file mode 100644 index 00000000..0cd26c9a --- /dev/null +++ b/problem2.py @@ -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] + \ No newline at end of file