Minimum Falling Path Sum (LeetCode 931): Solution and Explanation
In an n × n matrix, a falling path starts at any cell in the top row and moves down one row at a time, each time to the cell directly below or one of its two diagonal neighbors. Return the smallest possible sum of such a path.
Watch it run step by step
Try your own input and write the code →Intuition
The brute force starts a path at every top-row cell and branches three ways at every row, giving about n × 3 to the n - 1 paths. Even a 10 × 10 matrix is already large, and the constraints go to 100. The repetition is the same as in other grid path problems: many paths pass through each cell, and the cheapest way to reach a cell does not depend on what happens below it.
So let dp[i][j] be the minimum sum of a falling path that ends at cell (i, j). A path can only arrive at (i, j) from (i - 1, j - 1), (i - 1, j) or (i - 1, j + 1), so dp[i][j] = matrix[i][j] + min of those three. In the first row, the path is just the cell itself. Cells in the first and last columns only have two possible parents, and the reference code handles that by treating the missing diagonal as infinity, so it is never chosen.
After filling every row, the path can end anywhere in the bottom row, so the answer is the minimum of the last row. This is almost the same as the Triangle problem, except that every row has the same width and there are three parents instead of two.
Because each row only depends on the row directly above it, you could keep just two rows. The reference code keeps a full copy of the matrix for clarity.
Approach: Row-by-row DP with three parents
- Copy the matrix into
dp, so the first row already holds the cost of paths that end there. - Loop over rows
ifrom 1 to n - 1 and columnsjfrom 0 to n - 1. - Read
left = dp[i - 1][j - 1]ifj > 0, otherwise infinity. - Read
up = dp[i - 1][j]andright = dp[i - 1][j + 1]ifj < n - 1, otherwise infinity. - Set
dp[i][j] = matrix[i][j] + min(left, up, right). - Return
min(dp[n - 1]).
Step-by-step example
matrix = [[2,1,3],[6,5,4],[7,8,9]]
- Row 0 stays [2, 1, 3].
- Row 1: column 0 is 6 + min(2, 1) = 7, column 1 is 5 + min(2, 1, 3) = 6 and column 2 is 4 + min(1, 3) = 5, giving [7, 6, 5].
- Row 2: column 0 is 7 + min(7, 6) = 13, column 1 is 8 + min(7, 6, 5) = 13 and column 2 is 9 + min(6, 5) = 14, giving [13, 13, 14].
- The minimum of the last row is 13, reached for example by 1, 5, 7 or by 1, 4, 8.
Code: Python, Java, C++
1class Solution:
2 def minFallingPathSum(self, matrix: List[List[int]]) -> int:
3 n = len(matrix)
4 dp = [row[:] for row in matrix]
5
6 for i in range(1, n):
7 for j in range(n):
8 left = dp[i-1][j-1] if j > 0 else float('inf')
9 up = dp[i-1][j]
10 right = dp[i-1][j+1] if j < n-1 else float('inf')
11 dp[i][j] = matrix[i][j] + min(left, up, right)
12
13 return min(dp[n-1])1class Solution {
2 public int minFallingPathSum(int[][] matrix) {
3 int n = matrix.length;
4 int[][] dp = new int[n][n];
5 for (int j = 0; j < n; j++) dp[0][j] = matrix[0][j];
6
7 for (int i = 1; i < n; i++) {
8 for (int j = 0; j < n; j++) {
9 int left = j > 0 ? dp[i-1][j-1] : Integer.MAX_VALUE;
10 int up = dp[i-1][j];
11 int right = j < n-1 ? dp[i-1][j+1] : Integer.MAX_VALUE;
12 dp[i][j] = matrix[i][j] + Math.min(left, Math.min(up, right));
13 }
14 }
15
16 int min = dp[n-1][0];
17 for (int j = 1; j < n; j++) min = Math.min(min, dp[n-1][j]);
18 return min;
19 }
20}1class Solution {
2public:
3 int minFallingPathSum(vector<vector<int>>& matrix) {
4 int n = matrix.size();
5 vector<vector<int>> dp = matrix;
6
7 for (int i = 1; i < n; i++) {
8 for (int j = 0; j < n; j++) {
9 int left = j > 0 ? dp[i-1][j-1] : INT_MAX;
10 int up = dp[i-1][j];
11 int right = j < n-1 ? dp[i-1][j+1] : INT_MAX;
12 dp[i][j] = matrix[i][j] + min({left, up, right});
13 }
14 }
15
16 return *min_element(dp[n-1].begin(), dp[n-1].end());
17 }
18};Time and space complexity
O(n²)
Each of the n² cells is computed once from three cells in the previous row.
O(n²)
The dp table is a full copy of the n × n matrix.
Other approaches
Rolling row DP (time O(n²), space O(n))
Keep only the previous row and build each new row from it, or update the matrix in place if modifying the input is allowed. Same time, much less memory.
Top-down memoized recursion (time O(n²), space O(n²))
Define best(i, j) as the cheapest path from cell (i, j) to the bottom and memoize it, then take the minimum over the top row. Same complexity with recursion overhead.
Common mistakes
- Reading
dp[i - 1][j - 1]whenj == 0in Python silently picks the last column instead of failing, producing a path that wraps around. - Returning
dp[n - 1][0]or the value under the best top cell instead of the minimum of the entire bottom row. - Greedily picking the smallest starting cell and then the smallest child each step, which can miss a cheaper path that starts higher.
- Initializing the out-of-range diagonals to 0 instead of infinity, which makes edge cells look cheaper than they are when values are positive.
Frequently asked questions
How is minimum falling path sum different from the triangle problem?
Both are top-to-bottom path DPs. Here every row has n cells and each cell has three possible parents, while in the triangle each row grows by one and there are only two parents.
Why is infinity used for edge cells?
A cell in the first or last column has only two parents. Using infinity for the missing one guarantees that the min ignores it without extra branches.
Can the space be reduced to O(n)?
Yes. Each row depends only on the row above it, so you can keep a single previous-row array, or modify the matrix in place if allowed.
What is the time complexity?
O(n²), since every cell in the n × n matrix is filled once with a constant amount of work.