Minimum Falling Path Sum
Minimum Falling Path Sum is LeetCode 931 (Medium). You get a square n × n matrix of integers, and some of them may be negative. A falling path picks one cell in the top row, then one cell in every row below it, ending in the bottom row.
- From column
j, the next cell must be in columnj - 1,jorj + 1of the next row. - The sum of a path is the total of its
ncells.
Return the smallest sum any falling path can have. Each row can triple the number of paths, so listing them is hopeless even at n = 100.
- n == matrix.length == matrix[i].length
- 1 <= n <= 100
- -100 <= matrix[i][j] <= 100
Intuition
Think about where a path was one row earlier. A path that ends on cell (i, j) came from at most three cells: up-left, straight up, or up-right. If we already know the cheapest fall that ends at each cell of row i - 1, then the cheapest fall ending at (i, j) is matrix[i][j] plus the smallest of those three.
The top row is free: a fall that ends there is just that one cell. Each later row is built from the finished row above it, so the whole matrix is covered in one pass. The path may stop in any column, so the answer is the smallest value in the last row, not one fixed cell.
Choose one item per row, where each choice limits the next one to nearby positions, and minimise the total: that is row-by-row dynamic programming. Triangle (LeetCode 120) and Minimum Falling Path Sum II (LeetCode 1289) change only which cells of the previous row are allowed.
Approach
Before reading on, decide what number you would store for each cell so that the next row can be computed from it alone, and how you would handle the first and last columns. Aim for O(n²) time and O(n) memory.
Start from a copy of the top row
Set prev = matrix[0][:]. A fall that ends in the top row has only that one cell, so these values are already final, and copying them leaves the input untouched.
Price each cell of the next row
For column j, start with prev[j], and also consider prev[j - 1] and prev[j + 1] when those columns exist. Write matrix[i][j] plus the smallest into a new array cur. The first and last columns have only two cells above them.
Move down one row
Set prev = cur and repeat for every row. Only the row directly above is ever read, so two arrays of n numbers are all the memory the loop needs.
Pick the best ending column
After the last row, prev[j] is the cheapest fall that ends in column j. Return min(prev), because the problem lets the path finish in any column of the bottom row.
Code in Python, C++ and Java
Common pitfalls
Overwriting the row you are still reading
for j in range(n):
best = min(prev[max(j - 1, 0):j + 2])
prev[j] = matrix[i][j] + bestfor j in range(n):
best = min(prev[max(j - 1, 0):j + 2])
cur[j] = matrix[i][j] + best
prev = curBy the time column j + 1 is priced, prev[j] already holds the new row's value, so a cell can count two cells from the same row. On [[-1,-2,-2],[-3,-1,0],[1,0,0]] this returns -6 instead of -5.
Letting column -1 wrap around
best = min(prev[j - 1], prev[j], prev[j + 1] if j + 1 < n else prev[j])
best = prev[j]
if j > 0:
best = min(best, prev[j - 1])
if j < n - 1:
best = min(best, prev[j + 1])In Python prev[-1] is the last column, so column 0 silently reads a cell on the far side instead of failing. On [[3,0,-3],[-1,0,1],[0,-4,2]] this returns -8 instead of -7.
Starting the answer at 0
ans = 0
for v in prev:
ans = min(ans, v)ans = min(prev)
A falling path must use one cell in every row, so its sum is often positive. On [[2,1,3],[6,5,4],[7,8,9]] the 0 is never beaten and the result is 0 instead of 13.
Edge cases
[[-7]]The loop over later rows never runs, and the answer is the minimum of the top row, -7.
Complexity
n numbers are stored at any moment.Minimum Falling Path Sum FAQ
How is this different from Minimum Path Sum?
Minimum Path Sum (LeetCode 64) starts in a fixed corner, moves right or down, and ends in a fixed corner, so each cell has two sources and the answer is one cell. Here the path may start and end in any column, each cell has up to three sources directly above it, and the answer is the minimum over a whole row.
What changes in Minimum Falling Path Sum II?
In LeetCode 1289 the next cell may be in any column except the same one. Keep the smallest and second-smallest values of the row above: a cell adds the smallest unless it sits in that same column, in which case it adds the second-smallest. That keeps the work at O(n²).
Why not start at the smallest cell of the top row?
The cheapest start can lead into expensive rows. In [[1,2,3],[9,9,1],[9,9,9]] starting at 1 forces 9 and 9 for a total of 19, while starting at 2 allows 2 + 1 + 9 = 12. The row-by-row table keeps every start alive until the end.