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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code