Minimum Falling Path Sum - Problem

Imagine you're a ball rolling down a triangular mountain, and at each level you can only move to adjacent positions in the row below. Your goal is to find the path that gives you the minimum total cost as you fall from the top to the bottom.

Given an n × n matrix of integers, return the minimum sum of any falling path through the matrix. A falling path:

  • Starts at any element in the first row
  • At each step, moves to an adjacent element in the next row
  • From position (row, col), you can move to (row + 1, col - 1), (row + 1, col), or (row + 1, col + 1)

Example: In matrix [[2,1,3],[6,5,4],[7,8,9]], the path 1 → 5 → 7 gives sum = 13, which is minimal.

Input & Output

example_1.py — Standard Case
$ Input: matrix = [[2,1,3],[6,5,4],[7,8,9]]
Output: 13
💡 Note: The optimal path is 1→5→7 with sum = 1+5+7 = 13. Other paths like 2→6→7 (sum=15) or 3→4→9 (sum=16) have larger sums.
example_2.py — Negative Numbers
$ Input: matrix = [[-19,57],[-40,-5]]
Output: -59
💡 Note: The optimal path is -19→(-40) with sum = -19+(-40) = -59. The alternative path -19→(-5) gives sum = -24, which is larger.
example_3.py — Single Element
$ Input: matrix = [[100]]
Output: 100
💡 Note: With only one element, there's only one possible path, so the minimum sum is 100.

Constraints

  • n == matrix.length == matrix[i].length
  • 1 ≤ n ≤ 100
  • -100 ≤ matrix[i][j] ≤ 100

Visualization

Tap to expand
Minimum Falling Path Sum INPUT n × n matrix 2 1 3 6 5 4 7 8 9 From (row, col) can move to: (row+1, col-1) (row+1, col) (row+1, col+1) cell [[2,1,3],[6,5,4],[7,8,9]] ALGORITHM STEPS 1 Initialize DP Copy first row as base 2 Process Each Row From row 1 to n-1 3 Update DP Values dp[i][j] = matrix[i][j] + min(dp[i-1][j-1..j+1]) 4 Find Minimum Return min of last row DP Table Evolution: After row 0: 2 1 3 After row 1: 7 6 5 After row 2 (final): 13 13 14 min(13, 13, 14) = 13 FINAL RESULT Optimal Falling Path 2 1 3 6 5 4 7 8 9 Path Calculation: 1 + 5 + 7 = 13 [OK] Minimum Sum Found Key Insight: Dynamic Programming builds optimal solutions bottom-up. Each cell stores the minimum sum to reach it from the top. At each position, we only need to consider 3 possible predecessors: directly above, above-left, and above-right. Time: O(n^2), Space: O(n) with optimization. TutorialsPoint - Minimum Falling Path Sum | Optimal DP Solution
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
125.0K Views
High Frequency
~15 min Avg. Time
2.9K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen