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 @@
# problem 1

# https://leetcode.com/problems/delete-and-earn/


class Solution:
def deleteAndEarn(self, nums: List[int]) -> int:
points = {}
max_val = 0
# find the max val and count all the number
for num in nums:
if num in points:
points[num]+=num
else:
points[num] = num
max_val = max(max_val,num)

dp=[0]*(max_val+1)
dp[1] = points[1] if 1 in points else 0

for num in range(2,max_val+1):
dp[num] = max(dp[num-1],dp[num-2]+points[num] if num in points else 0)
return dp[max_val]
19 changes: 19 additions & 0 deletions problem2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# probleme.py
# https://leetcode.com/problems/minimum-falling-path-sum/

class Solution:
def minFallingPathSum(self, matrix: List[List[int]]) -> int:
m = len(matrix)
for i in range(m-2,-1,-1):
for j in range(m):
l_val = float('inf')
m_val = float('inf')
r_val = float('inf')
# find l_val
if j-1 >= 0:
l_val = matrix[i+1][j-1]
m_val = matrix[i+1][j]
if j+1 < m:
r_val = matrix[i+1][j+1]
matrix[i][j] += min(l_val,m_val,r_val)
return min(matrix[0])