Minimum Falling Path Sum
Find the minimum sum path falling from the top row to the bottom row, moving straight down or one column diagonally each step.
Open on LeetCode ↗Intuition
The trap is forgetting that the diagonal moves are bounded at the edges. Column 0 has no upper-left parent to fall from, and the last column has no upper-right parent -- an unguarded min() over three fixed parent offsets either reads past the array bounds or, worse, silently wraps around and compares against the wrong side of the row entirely. The fix is to only include parents that actually exist: for column j, gather dp[j] always, dp[j-1] only if j-1 >= 0, and dp[j+1] only if j+1 < n. The invariant: dp[j] after processing row i is the minimum sum of a falling path that ends at column j in row i, built strictly from in-bounds parents in the row above.
Approach
Seed with the top row
dp starts as a copy of row 0 -- a path of length one that ends at each column simply costs that cell's own value.
Fill downward with guarded neighbors
For each row i from 1 to n-1, and each column j, collect the candidate parent values: dp[j] (straight down, always valid), dp[j-1] (only if j > 0), and dp[j+1] (only if j < n-1). Take the minimum of whichever candidates exist and add matrix[i][j].
Read the last row
After processing the final row, the answer is the minimum value across that row's dp -- the cheapest path could end at any column.
Solution & live demo
Edge cases
Only dp[0] and dp[1] are valid parents; dp[-1] is never included.
Only dp[n-2] and dp[n-1] are valid parents; dp[n] is never included.
The loop over rows never runs; the answer is the single cell's value.
min() still correctly finds the most negative total sum with no special handling needed.