-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathminimum-falling-path-sum.py
94 lines (85 loc) · 3.02 KB
/
minimum-falling-path-sum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 931. Minimum Falling Path Sum
# 🟠 Medium
#
# https://leetcode.com/problems/minimum-falling-path-sum/
#
# Tags: Array - Dynamic Programming - Matrix
import timeit
from typing import List
# The bottom-up dynamic programming solution stores the last row of
# minimum sums and uses it to compute the current row up to the last one
# then returns the minimum sum on the row.
#
# Time complexity: O(m*n) - The number of rows * columns, each cell will
# be visited once.
# Space complexity: O(m) - We use one row of values worth of extra
# memory.
#
# Runtime 232 ms Beats 70.91%
# Memory 14.7 MB Beats 91.12%
class BottomUpDP:
def minFallingPathSum(self, matrix: List[List[int]]) -> int:
# A row of minimum sums up to the one we are computing.
dp = matrix[0][::]
for i in range(1, len(matrix)):
# A new array to update the values for the current row.
row = dp[::]
for j in range(len(matrix[0])):
# The minimum value to add to this cell.
minimum = dp[j]
if j > 0 and dp[j - 1] < minimum:
minimum = dp[j - 1]
if j < len(matrix[0]) - 1 and dp[j + 1] < minimum:
minimum = dp[j + 1]
row[j] = matrix[i][j] + minimum
dp = row
return min(dp)
# If we are allowed to mutate the input matrix, we can optimize the
# memory complexity storing intermediate results directly in the matrix,
# iterate over the rows using the previous row's results to update the
# current one.
#
# Time complexity: O(m*n) - The number of rows * columns, each cell will
# be visited once.
# Space complexity: O(1) - We use constant space, but we mutate the
# input, which may not be allowed.
#
# Runtime 236 ms Beats 70.41%
# Memory 14.9 MB Beats 40.57%
class BottomUpDPO1:
def minFallingPathSum(self, matrix: List[List[int]]) -> int:
n = len(matrix)
for i in range(1, n):
for j in range(n):
matrix[i][j] += min(
matrix[i - 1][max(0, j - 1)],
matrix[i - 1][j],
matrix[i - 1][min(n - 1, j + 1)],
)
return min(matrix[-1])
def test():
executors = [
BottomUpDP,
BottomUpDPO1,
]
tests = [
[[[-19, 57], [-40, -5]], -59],
[[[2, 1, 3], [6, 5, 4], [7, 8, 9]], 13],
]
for executor in executors:
start = timeit.default_timer()
for _ in range(1):
for col, t in enumerate(tests):
sol = executor()
result = sol.minFallingPathSum(t[0])
exp = t[1]
assert result == exp, (
f"\033[93m» {result} <> {exp}\033[91m for"
+ f" test {col} using \033[1m{executor.__name__}"
)
stop = timeit.default_timer()
used = str(round(stop - start, 5))
cols = "{0:20}{1:10}{2:10}"
res = cols.format(executor.__name__, used, "seconds")
print(f"\033[92m» {res}\033[0m")
test()