From 45aa182cb6635c4192ef76451a2ab48d5cd5c9f7 Mon Sep 17 00:00:00 2001 From: prenastro Date: Mon, 6 Jul 2026 03:53:40 -0700 Subject: [PATCH] Completed s30 DP-3 --- delandearn.py | 23 +++++++++++++++++++++++ minfailingpathsum.py | 22 ++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 delandearn.py create mode 100644 minfailingpathsum.py diff --git a/delandearn.py b/delandearn.py new file mode 100644 index 00000000..3df1f22d --- /dev/null +++ b/delandearn.py @@ -0,0 +1,23 @@ +class Solution: + def deleteAndEarn(self, nums): + max_val = 0 + for num in nums: + max_val = max(max_val, num) + + arr = [0] * (max_val + 1) + for num in nums: + arr[num] += num + + prev = arr[0] + curr = max(arr[0], arr[1]) + + for i in range(2, max_val + 1): + temp = curr + curr = max(curr, arr[i] + prev) + prev = temp + + return curr + + +# TC - O(n+max(n)) +# SC - O(max(n)) \ No newline at end of file diff --git a/minfailingpathsum.py b/minfailingpathsum.py new file mode 100644 index 00000000..36261af1 --- /dev/null +++ b/minfailingpathsum.py @@ -0,0 +1,22 @@ +class Solution: + def minFallingPathSum(self, matrix: List[List[int]]) -> int: + n = len(matrix) + + prev = matrix[0][:] + + for i in range(1, n): + curr = [0] * n + + for j in range(n): + up = prev[j] + left = prev[j-1] if j > 0 else float('inf') + right = prev[j+1] if j < n-1 else float('inf') + + curr[j] = matrix[i][j] + min(up, left, right) + + prev = curr + + return min(prev) + + # TC - O(n^2) + # SC - O(n) \ No newline at end of file