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.

Visualization

Tap to expand
🎿 Minimum Falling Path Visualization157236489Optimal Ski RoutePath: 1 → 5 → 7Total Difficulty: 13Movement Rules:• Can move diagonally left ↙• Can move straight down ↓• Can move diagonally right ↘
Understanding the Visualization
1
Choose starting position
You can start skiing from any position in the top row
2
Follow movement rules
From each position, you can only move to three adjacent positions below
3
Build optimal subpaths
For each position, calculate the minimum cost to reach it from any valid previous position
4
Find global minimum
The answer is the minimum value among all possible ending positions
Key Takeaway
🎯 Key Insight: Use dynamic programming to build the solution incrementally - each position only needs to consider the three positions directly above it, leading to an efficient O(n²) solution.

Time & Space Complexity

Time Complexity
⏱️
O(n²)

We visit each cell exactly once, and there are n² cells in the matrix

n
2n
Quadratic Growth
Space Complexity
O(1)

We can solve in-place by modifying the input matrix, or use O(n) space for one additional row

n
2n
Linear Space

Constraints

  • n == matrix.length == matrix[i].length
  • 1 ≤ n ≤ 100
  • -100 ≤ matrix[i][j] ≤ 100
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