Published by Dimitris Kougioumtzis · Updated
Count paths from top-left to bottom-right, moving only right or down.
Each cell adds path counts from above and left; boundary rows and columns have one monotone route.
When to use Unique monotone grid paths
Counting obstacle-free right/down routes.
Choosing an alternative
Minimum-cost grid DP optimizes cost; A* permits different movement and obstacles.
Worked example: input and state changes
Example input
3,7
These checkpoints follow the actual example timeline. State details show the stored values and decisions; use the visualization to inspect every intervening step.
Step 0: Start with an empty grid.
State at this checkpoint
{ "values": [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], "active": [], "kind": "matrix", "columns": 7 }Step 6: Cell [0,5] has 1 paths.
State at this checkpoint
{ "values": [ 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], "active": [ 5 ], "kind": "matrix", "columns": 7 }Step 11: Cell [1,3] has 4 paths.
State at this checkpoint
{ "values": [ 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], "active": [ 10 ], "kind": "matrix", "columns": 7 }Step 17: Cell [2,2] has 6 paths.
State at this checkpoint
{ "values": [ 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6, 7, 1, 3, 6, 0, 0, 0, 0 ], "active": [ 16 ], "kind": "matrix", "columns": 7 }Step 22: Unique grid paths = 28.
State at this checkpoint
{ "values": [ 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6, 7, 1, 3, 6, 10, 15, 21, 28 ], "active": [], "kind": "matrix", "columns": 7 }
Expected final state
Unique grid paths = 28.
Complexity and input bounds
Family: Dynamic programming. Time complexity: O(rows × columns).
Space complexity: O(rows times columns) displayed table.
Working-space bounds describe algorithm state and any explicitly noted output, excluding the visualization's saved timeline. Recording frames can use more memory than running the function alone. Here n is sequence length, V and E are vertex and edge counts, and other parameters refer to the named input dimensions.
Rows,columns; each dimension from 1 to 10.
The playground validates these limits before starting. Downloaded functions are callable implementations, with parameter and output-buffer contracts in their comments; they do not all repeat the playground's input validation.
Common mistakes and limitations
Diagonal moves, blocked cells, and revisits are not included in this model.
Further reading
For background on algorithm analysis and the JavaScript function interface:
- MIT OpenCourseWare: algorithm design and analysis foundations
- MDN: JavaScript function parameters and return values
Editorial and testing approach · Browse all algorithm guides
Complete JavaScript implementation
1 2 3 4 5 6 7 8 9 10 11 | // Rows and columns each 1–10. Counts paths from top-left to bottom-right using right/down moves only. Reference snippets assume validated playground inputs. Fixed scratch buffers have 256 slots. Sorting variants may use a different traversal strategy than the animation. function algorithm(rows, columns) { let dp = Array(256).fill(0); for (let r = 0; r < rows; r++) { for (let c = 0; c < columns; c++) { let i = r * columns + c; if (r == 0 || c == 0) { dp[i] = 1; } else { dp[i] = dp[i - columns] + dp[i - 1]; } } } return dp[rows * columns - 1]; } |
Source
Foundational dynamic-programming paper. It establishes the general method, not the first publication or exact recurrence of this particular teaching example.
- The theory of dynamic programming
Richard Bellman (1954). Foundational method paper.