What if you could instantly know all possible routes in a city grid without walking them?
Why Unique Paths in Grid DP in DSA Javascript?
Imagine you are in a big city with a grid of streets. You want to walk from your home at the top-left corner to a 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 route 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 routes or count some twice.
Dynamic Programming helps by breaking the problem into smaller parts. It remembers how many ways to reach each cell, so you don't count paths again and again. This way, you quickly find the total paths without listing them all.
function countPaths(row, col) {
if (row === 0 && col === 0) return 1;
if (row < 0 || col < 0) return 0;
return countPaths(row - 1, col) + countPaths(row, col - 1);
}function uniquePaths(rows, cols) {
const dp = Array(rows).fill().map(() => Array(cols).fill(0));
for (let r = 0; r < rows; r++) dp[r][0] = 1;
for (let c = 0; c < cols; c++) dp[0][c] = 1;
for (let r = 1; r < rows; r++) {
for (let c = 1; c < cols; c++) {
dp[r][c] = dp[r - 1][c] + dp[r][c - 1];
}
}
return dp[rows - 1][cols - 1];
}This method lets you quickly find how many ways to reach any point in a grid, even if the grid is very large.
Delivery drivers use similar calculations to find how many routes they can take through city blocks to reach a destination efficiently.
Manual counting of paths is slow and error-prone.
Dynamic Programming stores results to avoid repeated work.
It efficiently calculates total unique paths in a grid.
Practice
Unique Paths in Grid DP ask you to find?Solution
Step 1: Understand the problem goal
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: Differentiate from other path problems
It is not about shortest path length, maximum sum, or obstacles count, but about counting unique paths.Final Answer:
The number of different ways to move from the top-left to bottom-right corner of a grid using only right and down moves. -> Option AQuick Check:
Unique paths count = number of ways to move right/down [OK]
- Confusing unique paths with shortest path length
- Thinking about obstacles when none are given
- Counting diagonal moves
dp of size m x n filled with zeros for the Unique Paths problem?Solution
Step 1: Understand array initialization in JavaScript
UsingArray.fromwith a mapping function creates independent inner arrays, 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 create inner arrays withundefinedelements instead of zeros (missing.fill(0)).const dp = Array.from({ length: m }, () => Array(n).fill(0));correctly creates independent arrays filled with zeros.Final Answer:
const dp = Array.from({ length: m }, () => Array(n).fill(0)); -> Option AQuick Check:
Use Array.from with arrow function for 2D array [OK]
- Using fill with array directly causing shared references
- Not filling inner arrays with zeros (elements undefined)
- Not initializing inner arrays separately
uniquePaths(3, 3)?
function uniquePaths(m, n) {
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];
}
console.log(uniquePaths(3, 3));Solution
Step 1: Initialize dp array and base cases
Set first row and first column to 1 because only one way to reach cells in first row or column.Step 2: Fill dp using sum of top and left cells
Calculate dp[1][1] = 1+1=2, dp[1][2] = 1+2=3, dp[2][1] = 2+1=3, dp[2][2] = 3+3=6.Final Answer:
6 -> Option BQuick Check:
Unique paths in 3x3 grid = 6 [OK]
- Not initializing first row and column to 1
- Adding instead of summing dp[i-1][j] and dp[i][j-1]
- Off-by-one errors in loops
uniquePaths(3, 3). What is the bug?
function uniquePaths(m, n) {
const dp = Array(m).fill(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: Analyze dp initialization
UsingArray(m).fill(Array(n).fill(0))makes all rows point to the same inner array, so updating one row updates all rows.Step 2: Understand effect on dp updates
Setting dp[i][0] = 1 changes the same array for all rows. During table filling, updates overwrite values from earlier rows (e.g., dp[1][1] becomes 3 instead of 2), causing incorrect intermediate values.Final Answer:
Using fill with array causes all rows to reference the same array. -> Option CQuick Check:
Shared references cause wrong dp updates [OK]
- Using fill with array causing shared references
- Incorrect loop ranges
- Misplaced return statement
obstacleGrid where 1 means obstacle and 0 means free cell, how would you modify the Unique Paths DP solution to count unique paths avoiding obstacles?Solution
Step 1: Initialize dp considering obstacles
Set dp[0][0] to 1 only if the start cell has no obstacle; otherwise 0.Step 2: Update dp cells with obstacle check
For each cell, if obstacleGrid[i][j] is 1, dp[i][j] = 0 (no path). Else dp[i][j] = dp[i-1][j] + dp[i][j-1].Step 3: Return dp[m-1][n-1]
This gives the count of unique paths avoiding obstacles.Final Answer:
Initialize dp with zeros, set dp[0][0] = 1 if no obstacle, then for each cell dp[i][j] = 0 if obstacle else dp[i-1][j] + dp[i][j-1]. -> Option DQuick Check:
Obstacle cells block paths by setting dp to 0 [OK]
- Ignoring obstacles in dp calculation
- Not initializing start cell properly
- Using recursion without memoization causing inefficiency
Ready to prove it?
Pass the test and this pattern moves unknown
