Minimum Falling Path Sum

Medium Dynamic Programming 2D DP - max/min of last row Original on LeetCode

Minimum Falling Path Sum is a medium dynamic programming problem solved with the 2d dp - max/min of last row pattern. The best approach, optimal (dp row by row, answer = min of the last row), runs in O(n²) time and O(n) space. Below are 2 approaches in Java, from recursion from every start up.

Problem

A falling path starts at any cell of the first row and moves down one row at a time, to the cell directly below or diagonally left or right. Return the minimum sum of any falling path through the n × n matrix.

Examples

Example 1

Input
matrix = [[2,1,3],[6,5,4],[7,8,9]]
Output
13
Why
1 → 5 → 7 or 1 → 4 → 8.

Example 2

Input
matrix = [[-19,57],[-40,-5]]
Output
-59

Constraints

  • 1 <= n <= 100; square matrix; values from -100 to 100.
  • From (r, c) you can move to (r + 1, c - 1), (r + 1, c) or (r + 1, c + 1).

Animated walkthrough

A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.

Concepts first, then the problem and every approach, step by step.

Space to play or pause · ← → to jump a step · click or drag the bar to seek

Solutions

Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the Java code.

ApproachTimeSpace
Recursion from every startO(n · 3ⁿ)O(n)
Optimal (DP row by row, answer = min of the last row)O(n²)O(n)

1Recursion from every start

TimeO(n · 3ⁿ)
SpaceO(n)

For each starting column in the top row, try all three moves recursively.

  1. fall(r, c) = matrix[r][c] + min over the three columns below.
  2. Answer = min over c of fall(0, c).
Java
class Solution {
    public int minFallingPathSum(int[][] matrix) {
        int best = Integer.MAX_VALUE;
        for (int c = 0; c < matrix.length; c++) best = Math.min(best, fall(matrix, 0, c));
        return best;
    }

    private int fall(int[][] m, int r, int c) {
        if (c < 0 || c >= m.length) return Integer.MAX_VALUE;
        if (r == m.length - 1) return m[r][c];
        int below = Math.min(fall(m, r + 1, c), Math.min(fall(m, r + 1, c - 1), fall(m, r + 1, c + 1)));
        return m[r][c] + below;
    }
}

2Optimal (DP row by row, answer = min of the last row)

TimeO(n²)
SpaceO(n)

dp[c] = cheapest falling path ending at column c of the current row. Each new row takes the best of the three cells above, then the answer is the minimum over the last row.

  1. dp = first row.
  2. next[c] = matrix[r][c] + min(dp[c - 1], dp[c], dp[c + 1]) within bounds.
  3. Return min(dp).
Java
class Solution {
    public int minFallingPathSum(int[][] matrix) {
        int n = matrix.length;
        int[] dp = matrix[0].clone();
        for (int r = 1; r < n; r++) {
            int[] next = new int[n];
            for (int c = 0; c < n; c++) {
                int best = dp[c];
                if (c > 0) best = Math.min(best, dp[c - 1]);
                if (c < n - 1) best = Math.min(best, dp[c + 1]);
                next[c] = matrix[r][c] + best;
            }
            dp = next;
        }
        int ans = Integer.MAX_VALUE;
        for (int x : dp) ans = Math.min(ans, x);
        return ans;
    }
}

Edge cases to test

  • Edge columns have only two moves
  • n = 1

Hints

Hint 1

Unlike Minimum Path Sum there is no single end cell: the answer is the minimum over the whole last row.

FAQ

What is the best time complexity for Minimum Falling Path Sum?

Optimal (DP row by row, answer = min of the last row) runs in O(n²) time and O(n) extra space.

Which pattern does Minimum Falling Path Sum use?

It is a dynamic programming problem that uses the 2d dp - max/min of last row pattern. Other problems with the same pattern: Triangle, Geek's Training.

Is there a brute force solution for Minimum Falling Path Sum?

Yes. Recursion from every start takes O(n · 3ⁿ) time and O(n) space. For each starting column in the top row, try all three moves recursively.

Which edge cases should I test for Minimum Falling Path Sum?

Edge columns have only two moves; n = 1.