Overview
Grid DP is extremely common. The state `dp[r][c]` typically represents the cost, max profit, or number of ways to reach cell `(r, c)`.
Loading...
Loading Curriculum...
Loading Subject...
Loading Topic...
Loading Lesson...
Loading Lab...
Explore how to solve grid traversal problems using dynamic programming by analyzing state transitions from adjacent cells.
Grid DP is extremely common. The state `dp[r][c]` typically represents the cost, max profit, or number of ways to reach cell `(r, c)`.
Since we usually can only move Right or Down, we can guarantee that when calculating `dp[r][c]`, both `dp[r-1][c]` and `dp[r][c-1]` are already computed.
The starting cell `(0, 0)` is initialized based on the problem (e.g. `1` for path counting, `grid[0][0]` for path sum). Boundaries need bounds checking.
Because `dp[r][c]` only depends on the current row and the previous row, we can often optimize the O(R * C) space down to O(C) by keeping a 1D array.