Skip to content

Commit 54f8386

Browse files
committed
🌇 min falling path sum - lt medium - 100 pass
1 parent 624f200 commit 54f8386

File tree

1 file changed

+31
-0
lines changed
  • ProblemSolving/minimumFallingPathSum

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution(object):
2+
def minFallingPathSum(self, A):
3+
"""
4+
:type A: List[List[int]]
5+
:rtype: int
6+
"""
7+
8+
# Logic2: 100 pass only 5%faster.. better one yet?
9+
# * Iterate through row and columsn (i,j)
10+
# * For each column, the previous possible combinations are i-1, j-1 and i-1, j and i-1, j+1
11+
# * Get the minimum of those combinations and add as we iterate to the last row
12+
# * Return the minimum possible from the last row sums.
13+
# * Reference: https://leetcode.com/problems/minimum-falling-path-sum/discuss/186843/Python-easy-4-liner
14+
15+
for i in range(1, len(A)): # Row iteration
16+
for j in range(len(A)): # Column iteration
17+
A[i][j] += min(A[i-1][j-1] if j-1 >= 0 else float("inf"), A[i-1][j], A[i-1][j+1] if j+1 < len(A) else float("inf"))
18+
print A[-1]
19+
return min(A[-1])
20+
21+
"""
22+
# Logic 1: Wrong
23+
# * This takes into account the paths that cannot be traversed in a square like 1,4,9
24+
min_path = float('inf')
25+
import itertools
26+
for path in itertools.product(*A):
27+
min_path = min(sum(path), min_path)
28+
return min_path
29+
"""
30+
31+

0 commit comments

Comments
 (0)