📖 Scenario: Imagine you are helping a robot find how many different ways it can move from the top-left corner to the bottom-right corner of a grid. The robot can only move right or down.
🎯 Goal: Build a program that calculates the number of unique paths the robot can take in a grid of size m x n using dynamic programming.
📋 What You'll Learn
Create a 2D list called dp to store the number of ways to reach each cell
Use two variables m and n for grid dimensions
Fill the dp table using the rule: ways to reach a cell = ways from above + ways from left
Print the number of unique paths to reach the bottom-right cell
💡 Why This Matters
🌍 Real World
Robots and automated systems often need to find paths in grids or maps. This method helps count possible routes efficiently.
💼 Career
Understanding dynamic programming and grid traversal is important for software roles involving algorithms, robotics, and game development.
Progress0 / 4 steps
1
Create grid size variables
Create two variables called m and n and set them to 3 and 4 respectively to represent the grid size.
DSA Python
Hint
Use m = 3 and n = 4 exactly to set the grid size.
2
Initialize the dp table
Create a 2D list called dp with m rows and n columns, where each element is initialized to 1.
DSA Python
Hint
Use list comprehension to create dp with all values set to 1.
3
Fill dp table with unique path counts
Use a nested for loop with variables i and j to iterate over dp starting from i = 1 and j = 1. Update dp[i][j] to be the sum of dp[i-1][j] and dp[i][j-1].
DSA Python
Hint
Start loops from 1 because the first row and column are already set to 1.
4
Print the number of unique paths
Print the value of dp[m-1][n-1] which represents the number of unique paths to the bottom-right corner.
DSA Python
Hint
The answer is the value in the bottom-right cell of dp.
Practice
(1/5)
1. What does the problem "Unique Paths in Grid DP" ask you to find? Unique Paths means:
easy
A. The maximum sum path from top-left to bottom-right in a grid
B. The shortest path length from top-left to bottom-right in a grid
C. The number of ways to move from top-left to bottom-right in a grid moving only right or down
D. The number of obstacles in the grid
Solution
Step 1: Understand the problem goal
The problem asks for counting all possible ways to reach the bottom-right cell from the top-left cell by moving only right or down.
Step 2: Clarify movement constraints
Only moves allowed are right or down, so paths are unique sequences of these moves.
Final Answer:
The number of ways to move from top-left to bottom-right in a grid moving only right or down -> Option C
Quick Check:
Unique Paths = Count of right/down moves sequences [OK]
Hint: Count paths by moving only right or down [OK]
Common Mistakes:
Thinking about shortest path length instead of count
Including diagonal moves
Confusing with maximum sum path
2. Which of the following Python code snippets correctly initializes a 2D list dp of size m x n with all values set to 1?
easy
A. dp = [[1]*m for _ in range(n)]
B. dp = [[1]*n for _ in range(m)]
C. dp = [[1]*n]*m
D. dp = [1]*m*n
Solution
Step 1: Understand 2D list initialization
We want a list with m rows and n columns, each cell initialized to 1.
Step 2: Check each option
dp = [[1]*n for _ in range(m)] uses list comprehension correctly to create m lists each of length n. dp = [[1]*m for _ in range(n)] swaps m and n. dp = [[1]*n]*m creates shallow copies causing aliasing issues. dp = [1]*m*n creates a flat list, not 2D.
Final Answer:
dp = [[1]*n for _ in range(m)] -> Option B
Quick Check:
Correct 2D list init = dp = [[1]*n for _ in range(m)] [OK]
Hint: Use list comprehension for 2D lists to avoid aliasing [OK]
Common Mistakes:
Using [[1]*n]*m causing all rows to reference same list
Swapping m and n dimensions
Creating flat list instead of 2D
3. What is the output of the following code?
def unique_paths(m, n):
dp = [[1]*n for _ in range(m)]
for i in range(1, m):
for j in range(1, n):
dp[i][j] = dp[i-1][j] + dp[i][j-1]
return dp[m-1][n-1]
print(unique_paths(3, 3))
medium
A. 6
B. 3
C. 9
D. 1
Solution
Step 1: Understand DP table filling
dp[i][j] stores number of unique paths to cell (i,j). First row and column are 1 because only one way to reach them.
4. Find the error in the following code for unique paths and select the fix:
def unique_paths(m, n):
dp = [[0]*n]*m
for i in range(m):
dp[i][0] = 1
for j in range(n):
dp[0][j] = 1
for i in range(1, m):
for j in range(1, n):
dp[i][j] = dp[i-1][j] + dp[i][j-1]
return dp[m-1][n-1]
medium
A. Return dp[m][n] instead of dp[m-1][n-1]
B. Change loops to start from 0 instead of 1
C. Initialize dp with ones instead of zeros
D. Change dp initialization to dp = [[0]*n for _ in range(m)]
Solution
Step 1: Identify dp initialization problem
Using dp = [[0]*n]*m creates shallow copies of the same row, so updating one row updates all rows.
Step 2: Fix dp initialization
Use list comprehension dp = [[0]*n for _ in range(m)] to create independent rows.
Final Answer:
Change dp initialization to dp = [[0]*n for _ in range(m)] -> Option D
Quick Check:
Independent rows fix aliasing issue [OK]
Hint: Use list comprehension for 2D list to avoid shared references [OK]
Common Mistakes:
Using [[0]*n]*m causing all rows to be same object
Changing loop ranges unnecessarily
Returning wrong dp index
5. Given a grid with obstacles represented by 1s and empty spaces by 0s, how would you modify the unique paths DP solution to count paths avoiding obstacles? Choose the correct approach:
hard
A. Set dp[i][j] = 0 if grid[i][j] == 1; else dp[i][j] = dp[i-1][j] + dp[i][j-1], initialize dp[0][0] = 1 if no obstacle
B. Ignore obstacles and run normal unique paths DP
C. Set dp[i][j] = 1 for obstacles and 0 otherwise
D. Use recursion without memoization to handle obstacles
Solution
Step 1: Handle obstacles in DP initialization
If a cell has an obstacle (1), no paths can go through it, so dp[i][j] = 0.
Step 2: Update dp values considering obstacles
For empty cells (0), dp[i][j] = sum of paths from top and left neighbors if they are not blocked.
Final Answer:
Set dp[i][j] = 0 if grid[i][j] == 1; else dp[i][j] = dp[i-1][j] + dp[i][j-1], initialize dp[0][0] = 1 if no obstacle -> Option A
Quick Check:
Obstacles block paths, so dp = 0 there [OK]
Hint: Set dp to 0 at obstacles, sum neighbors otherwise [OK]
Common Mistakes:
Ignoring obstacles in DP calculation
Setting dp=1 at obstacle cells
Using recursion without memo causing inefficiency
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.