Overview
This is standard 2D Grid DP with a twist: you can start ANYWHERE in the top row, and you must reach the bottom row. At each step, you can fall to 3 possible adjacent columns.
Loading...
Loading Curriculum...
Loading Subject...
Loading Topic...
Loading Lesson...
Loading Lab...
Find the maximum sum of a falling path through a matrix. You can start at any cell in the top row and fall down-left, down, or down-right.
This is standard 2D Grid DP with a twist: you can start ANYWHERE in the top row, and you must reach the bottom row. At each step, you can fall to 3 possible adjacent columns.
For any cell `(r, c)`, the maximum path to reach it is its own value plus the maximum of the three valid cells directly above it: `(r-1, c-1)`, `(r-1, c)`, or `(r-1, c+1)`.
When calculating `upLeft` or `upRight` for edge columns, the out-of-bounds cells are treated as `-Infinity` so they are never picked by the `Math.max` function.
Because the path can end anywhere in the bottom row, the final answer is the maximum value in the entire last row of the `dp` array.