What if you could count thousands of paths in seconds without writing them all down?
Why Unique Paths in Grid DP in DSA Typescript?
Imagine you are in a big city grid with streets laid out in rows and columns. You want to walk from your home at the top-left corner to your friend's house at the bottom-right corner. You can only walk right or down. Counting all possible ways by hand is like listing every single path on a huge map.
Trying to count all paths manually is slow and confusing. As the grid grows, the number of paths grows very fast, making it impossible to keep track without mistakes. You might forget some paths or count some twice. It's like trying to count every grain of sand on a beach by hand.
Dynamic Programming (DP) breaks this big problem into smaller, easy steps. It remembers how many ways to reach each spot in the grid, building up the answer step-by-step. This way, you don't have to list all paths, just add up smaller results. It's like using a map with notes on how many ways to get to each intersection.
function countPaths(m: number, n: number): number {
// Try all paths recursively (slow)
if (m === 1 || n === 1) return 1;
return countPaths(m - 1, n) + countPaths(m, n - 1);
}function countPaths(m: number, n: number): number {
const dp: number[][] = Array(m).fill(0).map(() => Array(n).fill(1));
for (let i = 1; i < m; i++) {
for (let j = 1; j < n; j++) {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
return dp[m - 1][n - 1];
}It lets you quickly find how many ways to reach any point in a grid without listing every path, saving time and avoiding mistakes.
Delivery drivers use similar calculations to find how many routes they can take through city blocks when they can only move right or down, helping them plan efficient deliveries.
Manual counting of paths is slow and error-prone.
Dynamic Programming breaks the problem into smaller parts and remembers results.
This method quickly calculates total unique paths in a grid.
Practice
Solution
Step 1: Understand the problem statement
The problem asks for counting all possible paths from the top-left corner to the bottom-right corner of a grid, moving only right or down.Step 2: Identify the key movement constraints
Only moves to the right or down are allowed, so the problem counts unique sequences of these moves.Final Answer:
The number of ways to move from the top-left to the bottom-right corner of a grid using only right and down moves. -> Option AQuick Check:
Unique paths = count of right/down moves sequences [OK]
- Confusing unique paths with shortest path length
- Including diagonal moves
- Counting obstacles instead of paths
Solution
Step 1: Understand how to create independent rows
Using Array.from with a mapping function creates new arrays for each row, avoiding shared references.Step 2: Analyze each option
const dp = Array(m).fill(Array(n).fill(0)); shares the same inner array reference for all rows, causing bugs. Options C and D pass an array (not a function) to .map(), causing TypeError. const dp = Array.from({ length: m }, () => Array(n).fill(0)); correctly creates independent rows filled with zeros.Final Answer:
const dp = Array.from({ length: m }, () => Array(n).fill(0)); -> Option DQuick Check:
Use Array.from with a function for 2D arrays [OK]
- Using fill with a single array causes shared references
- Passing non-function to .map() causes TypeError
- Not initializing inner arrays separately
function uniquePaths(m: number, n: number): number {
const dp = Array.from({ length: m }, () => Array(n).fill(1));
for (let i = 1; i < m; i++) {
for (let j = 1; j < n; j++) {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
return dp[m - 1][n - 1];
}
console.log(uniquePaths(3, 3));Solution
Step 1: Initialize dp array with 1s
Each cell in the first row and first column is 1, representing only one path to those cells.Step 2: Fill dp using sum of top and left cells
For each cell dp[i][j], sum dp[i-1][j] (top) and dp[i][j-1] (left). For 3x3, dp[2][2] = 6.Final Answer:
6 -> Option CQuick Check:
Unique paths in 3x3 grid = 6 [OK]
- Starting loops from 0 instead of 1
- Not initializing first row and column to 1
- Returning wrong dp cell
function uniquePaths(m: number, n: number): number {
const dp = Array.from({ length: m }, () => Array(n).fill(0));
for (let i = 0; i < m; i++) dp[i][0] = 1;
for (let j = 0; j < n; j++) dp[0][j] = 1;
for (let i = 1; i < m; i++) {
for (let j = 1; j < n; j++) {
dp[i][j] = dp[i - 1][j] * dp[i][j - 1];
}
}
return dp[m - 1][n - 1];
}Solution
Step 1: Identify the dp update formula
The code uses multiplication (*) to combine paths, which is incorrect because paths add up, not multiply.Step 2: Correct the dp update to addition
Replace dp[i - 1][j] * dp[i][j - 1] with dp[i - 1][j] + dp[i][j - 1] to sum paths from top and left.Final Answer:
Change multiplication (*) to addition (+) in dp update. -> Option AQuick Check:
Paths combine by addition, not multiplication [OK]
- Using multiplication instead of addition
- Incorrect dp initialization
- Wrong loop start indices
const grid = [ [0,0,0], [0,1,0], [0,0,0] ];
Solution
Step 1: Mark obstacle cells as unreachable
If grid[i][j] is 1 (obstacle), set dp[i][j] = 0 to block paths through it.Step 2: Calculate dp for free cells
For cells without obstacles, dp[i][j] = dp[i-1][j] + dp[i][j-1], summing paths from top and left.Final Answer:
Set dp[i][j] = 0 if grid[i][j] is 1; else dp[i][j] = dp[i-1][j] + dp[i][j-1]. -> Option BQuick Check:
Obstacles block paths by setting dp to zero [OK]
- Ignoring obstacles in dp calculation
- Setting dp to 1 on obstacles
- Multiplying paths instead of adding
Ready to prove it?
Pass the test and this pattern moves unknown
