From 994b7707249b8f946bdcca772cdb607a4713530d Mon Sep 17 00:00:00 2001 From: Adithya Vinayak Date: Sat, 25 Apr 2026 17:13:12 -0400 Subject: [PATCH] working solution --- problem1.py | 23 +++++++++++++++++++++++ problem2.py | 19 +++++++++++++++++++ 2 files changed, 42 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..573d131a --- /dev/null +++ b/problem1.py @@ -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] \ No newline at end of file diff --git a/problem2.py b/problem2.py new file mode 100644 index 00000000..05395feb --- /dev/null +++ b/problem2.py @@ -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]) \ No newline at end of file