Counting Grid Paths with Dynamic Programming
Add counts from the allowed predecessor cells to get the count at the current cell.
Core Intuition & How It Works
Grid dynamic programming counts paths or minimizes path cost across an R × C grid where moves go right or down. Each cell combines answers from the cell directly above and the cell directly to its left.
In the Byte Sized Algorithms interactive classroom, you can step forward and backward through this algorithm with your keyboard while watching the visual diagram, memory state, and highlighted pseudocode update together.
Time & Space Complexity
| Metric | Complexity |
|---|---|
| Time Complexity | O(R × C) |
| Space Complexity | O(R × C) or O(C) rolling |
| Topic Chapter | Chapter 13: Dynamic Programming |
| Difficulty Level | Advanced |
Key Pattern to Recognize
Use dp[r][c] = dp[r - 1][c] + dp[r][c - 1] (or min/max for weighted paths), zeroing blocked obstacle cells.
Common Pitfalls & Edge Cases
Forgetting to stop propagating 1s along the first row or column after hitting an obstacle.
Real-World & Technical Interview Applications
Robot path counting, minimum path sum, seam carving in images, and warehouse routing.