Skip to content

Commit f6d47f1

Browse files
authored
Create minimum-falling-path-sum-ii.py
1 parent 15ab7b3 commit f6d47f1

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

Python/minimum-falling-path-sum-ii.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Time: O(m * n)
2+
# Space: O(1)
3+
4+
import heapq
5+
6+
7+
class Solution(object):
8+
def minFallingPathSum(self, arr):
9+
"""
10+
:type arr: List[List[int]]
11+
:rtype: int
12+
"""
13+
for i in xrange(1, len(arr)):
14+
smallest_two = heapq.nsmallest(2, arr[i-1])
15+
for j in xrange(len(arr[0])):
16+
arr[i][j] += smallest_two[1] if arr[i-1][j] == smallest_two[0] else smallest_two[0]
17+
return min(arr[-1])

0 commit comments

Comments
 (0)