Minimum Falling Path Sum
Implement minFallingPathSum
Given a square grid, find the minimum possible sum of any "falling path" from the top row to the bottom row — a path that starts at any cell in the top row and, at every later row, moves either straight down or diagonally into the column immediately to its left or right.
Every cell's cheapest finish depends only on the (up to three) cells directly above it that could have fallen into it, so working row by row from the top down — where the top row's cheapest finish is simply its own value — lets each row be resolved using results already computed for the row before it. Once every row has been swept through, the smallest value on the last row is the answer, since the path is free to land in whichever column ends up cheapest.
Example 1:
Input: matrix = [[3,4,1],[8,2,6],[5,9,3]]
Output: 6
Example 2:
Input: matrix = [[2,5],[4,1]]
Output: 3
Example 3:
Input: matrix = [[7]]
Output: 7
+ 7 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ matrix.length ≤ 20 (a square n×n grid) - ●
-100 ≤ matrix[i][j] ≤ 100
matrix =
[[3,4,1], [8,2,6], [5,9,3]]