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
23 changes: 23 additions & 0 deletions Problem1.py
Original file line number Diff line number Diff line change
@@ -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]
24 changes: 24 additions & 0 deletions Problem2.py
Original file line number Diff line number Diff line change
@@ -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