From 428c2c5d3c6b4213591d548b29f68f31412156ab Mon Sep 17 00:00:00 2001 From: pranjay01 Date: Sun, 22 Feb 2026 09:41:14 -0800 Subject: [PATCH] DP 1 done --- Problem1.py | 23 +++++++++++++++++++++++ Problem2.py | 24 ++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 Problem1.py create mode 100644 Problem2.py diff --git a/Problem1.py b/Problem1.py new file mode 100644 index 00000000..58cd0741 --- /dev/null +++ b/Problem1.py @@ -0,0 +1,23 @@ +#Coin Change + +import math +class Solution: + def coinChange(self, coins: List[int], amount: int) -> int: + prevArr = [math.inf] * (amount+1) + length = len(prevArr) + for coin in coins: + for i in range(0,length): + if i == 0: + prevArr[i] = 0 + continue + if coin > i: + #noChose, do nothing, already should have min in that index on array, if there is a possible path + continue + else: + #compare between choose and no choose option, in case the possible ways will be 1(for choosing) + min possible ways for achieving remaining value + minPossible = min(prevArr[i], (1+prevArr[i-coin])) + prevArr[i] = minPossible + if prevArr[length-1] == math.inf: + return -1 + else: + return prevArr[length-1] \ No newline at end of file diff --git a/Problem2.py b/Problem2.py new file mode 100644 index 00000000..a805d516 --- /dev/null +++ b/Problem2.py @@ -0,0 +1,24 @@ +#House Robber + +#keep on calculating the max possible sum till current house by comparing 2 options, if we choose current then add that with the max till the house +# 2 before and comapre it with just no choose category, in which you are just comparing max till prev house. For current house the max will become the +# max of these 2 choices +class Solution: + def rob(self, nums: List[int]) -> int: + totalHouses = len(nums) + if totalHouses == 1: + return nums[0] + + if totalHouses == 2: + return max(nums[0], nums[1]) + + # we just need max possible count for last 2 houses + prevHouseMaxCount = max(nums[0], nums[1]) + prevPrev = nums[0] + + for i in range(2,totalHouses): + choseCurrentMaxCount = nums[i] + prevPrev + prevPrev = prevHouseMaxCount + prevHouseMaxCount = max(choseCurrentMaxCount, prevHouseMaxCount) + + return prevHouseMaxCount \ No newline at end of file