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
73 changes: 73 additions & 0 deletions Problem_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
## Problem1 (https://leetcode.com/problems/coin-change/)


# Brute Force

'''
class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
result = self.getCoins(0, amount, coins)
if result == 1e9:
return -1
return result

def getCoins(self, index, amount, coins):
if index >= len(coins) or amount < 0:
return 1e9
if amount == 0:
return 0
not_choose = self.getCoins(index + 1, amount, coins)
choose = 1 + self.getCoins(index, amount - coins[index], coins)
return min(not_choose, choose)

'''

# DP using memoization (Top Down)
'''
class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
n = len(coins)
memo = [[-1] * (amount + 1) for _ in range(n)]
result = self.getCoins(0, amount, coins, memo)
if result == 1e9:
return -1
return result

def getCoins(self, index, amount, coins, memo):
if amount == 0:
return 0
if index >= len(coins) or amount < 0:
return 1e9
if memo[index][amount] != -1:
return memo[index][amount]
not_choose = self.getCoins(index + 1, amount, coins, memo)
choose = 1 + self.getCoins(index, amount - coins[index], coins, memo)
memo[index][amount] = min(not_choose, choose)
return memo[index][amount]

'''

# Tabulation

class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
n = len(coins)
INF = int(1e9)

memo = [[0] * (amount + 1) for _ in range(n + 1)]

for target in range(1, amount + 1):
memo[n][target] = INF

for index in range(n - 1, -1, -1):
for target in range(amount + 1):
not_choose = memo[index + 1][target]

choose = INF
if target >= coins[index]:
choose = 1 + memo[index][target - coins[index]]

memo[index][target] = min(not_choose, choose)

result = memo[0][amount]
return -1 if result == INF else result
71 changes: 71 additions & 0 deletions Problem_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
## Problem2 (https://leetcode.com/problems/house-robber/)

# Brute Force

'''

class Solution:
def rob(self, nums: List[int]) -> int:
return self.getMax(0, nums)

def getMax(self, index, nums):
if index >= len(nums):
return 0
not_robbed = self.getMax(index + 1, nums)
robbed = nums[index] + self.getMax(index + 2, nums)
return max(not_robbed, robbed)

'''

# Memoization (Top Down)
'''
class Solution:
def rob(self, nums: List[int]) -> int:
n = len(nums)
memo = [-1] * n
return self.getMax(0, nums, memo)

def getMax(self, index, nums, memo):
if index >= len(nums):
return 0
if memo[index] != -1:
return memo[index]
not_robbed = self.getMax(index + 1, nums, memo)
robbed = nums[index] + self.getMax(index + 2, nums, memo)
memo[index] = max(not_robbed, robbed)
return memo[index]

'''

# Tabulation (bottom up)

'''
class Solution:
def rob(self, nums: List[int]) -> int:
n = len(nums)
memo = [0] * (n + 2)
for index in range(n - 1, -1, -1):
not_robbed = memo[index + 1]
robbed = nums[index] + memo[index + 2]
memo[index] = max(not_robbed, robbed)
return memo[0]
'''

# Space Optimised Tabulation

class Solution:
def rob(self, nums):
n = len(nums)
memo_plus_1, memo_plus_2 = 0, 0
result = 0
for index in range(n - 1, -1, -1):
not_robbed = memo_plus_1
robbed = nums[index] + memo_plus_2
result = max(not_robbed, robbed)
memo_plus_2 = memo_plus_1
memo_plus_1 = result
return result

nums = [2,7,9,3,1]
sl = Solution()
print(sl.rob(nums))