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
88 changes: 88 additions & 0 deletions Problem1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# https://leetcode.com/problems/delete-and-earn/description/

# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No
# Recursive approach (exhaustive)

class Solution:
def deleteAndEarn(self, nums: List[int]) -> int:

if len(nums) == 1:
return nums[0]

maxNum = 0

for num in nums:
maxNum = max(maxNum, num)

arr = [0] * (maxNum+1)

for num in nums:
arr[num] += num

return self.helper(arr, 0, 0)

def helper(self, arr, i, earnings):

if i >= len(arr): return earnings

case0 = self.helper(arr, i+1, earnings)

case1 = self.helper(arr, i+2, earnings + arr[i])

return max(case0, case1)

# DP solution

class Solution:
def deleteAndEarn(self, nums: List[int]) -> int:
n = len(nums)

maxVal = 0
for n in nums:
maxVal = max(maxVal, n)

arr = [0] * (maxVal+1)
dp = [0] * (maxVal+1)

m = len(arr)

for num in nums:
arr[num] += num

dp[0] = arr[0]
dp[1] = max(arr[0], arr[1])

for i in range(2, maxVal+1):
dp[i] = max(dp[i-1], arr[i]+ dp[i-2])

return dp[m-1]

# DP using prev and curr

class Solution:
def deleteAndEarn(self, nums: List[int]) -> int:
n = len(nums)

maxVal = 0
for n in nums:
maxVal = max(maxVal, n)

arr = [0] * (maxVal+1)
dp = [0] * (maxVal+1)

m = len(arr)

for num in nums:
arr[num] += num

prev = arr[0]
curr = max(arr[0], arr[1])

for i in range(2, maxVal+1):
temp = curr
curr = max(curr, arr[i]+ prev)
prev = temp

return curr

126 changes: 126 additions & 0 deletions Problem2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# https://leetcode.com/problems/unique-paths/description/

# exhaustive approach
# TC: 2^(m+n)
# SC: O(m+n)
class Solution:
count = 0
def uniquePaths(self, m: int, n: int) -> int:
self.helper(0, 0, m, n)
return self.count

def helper(self, i, j, m, n):

if i == m-1 and j == n-1:
self.count += 1
return

if i == m or j == n:
return

# move right
self.helper(i, j+1, m, n)

# move down
self.helper(i+1, j, m, n)


# int based recursion
class Solution:
count = 0
def uniquePaths(self, m: int, n: int) -> int:
return self.helper(0, 0, m, n)

def helper(self, i, j, m, n):

if i == m-1 and j == n-1:
return 1

if i == m or j == n:
return 0

# move right
case0 = self.helper(i, j+1, m, n)

# move down
case1 = self.helper(i+1, j, m, n)

return case0 + case1

# DP solution with memoization using 2d matrix
# TC: O(m*n)
# SC: O(m*n)
class Solution:

def uniquePaths(self, m: int, n: int) -> int:
self.memo = [[0] * n for _ in range(m)]
return self.helper(0, 0, m, n)

def helper(self, i, j, m, n):

if i == m-1 and j == n-1:
return 1

if i == m or j == n:
return 0

if self.memo[i][j] != 0: return self.memo[i][j]

# move right
case0 = self.helper(i, j+1, m, n)

# move down
case1 = self.helper(i+1, j, m, n)

self.memo[i][j] = case0 + case1

return case0 + case1

# DP with tabulation (bottom-up)
# TC: O(m*n)
# SC: O(m*n)
class Solution:
def uniquePaths(self, m: int, n: int) -> int:

dp = [[0] * n for _ in range(m)]

for i in range(m-1, -1, -1):
for j in range(n-1, -1, -1):
if i == m-1 or j == n-1:
dp[i][j] = 1
else:
dp[i][j] = dp[i+1][j] + dp[i][j+1]

return dp[0][0]

# DP with tabulation (top-down)
# TC: O(m*n)
# SC: O(m*n)
class Solution:
def uniquePaths(self, m: int, n: int) -> int:

dp = [[0] * n for _ in range(m)]

for i in range(m):
for j in range(n):
if i == 0 or j == 0:
dp[i][j] = 1
else:
dp[i][j] = dp[i-1][j] + dp[i][j-1]

return dp[m-1][n-1]

# DP with 1D array
class Solution:
def uniquePaths(self, m: int, n: int) -> int:

dp = [0] * n

for i in range(m):
for j in range(n):
if i == 0 or j == 0:
dp[j] = 1
else:
dp[j] = dp[j] + dp[j-1]

return dp[n-1]