Unique Paths in Grid DP in DSA C# - Time & Space Complexity
We want to understand how the time needed to find unique paths in a grid grows as the grid size increases.
Specifically, how does the number of steps change when the grid gets bigger?
Analyze the time complexity of the following code snippet.
public int UniquePaths(int m, int n) {
int[,] dp = new int[m, n];
for (int i = 0; i < m; i++) dp[i, 0] = 1;
for (int j = 0; j < n; j++) dp[0, j] = 1;
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
dp[i, j] = dp[i - 1, j] + dp[i, j - 1];
}
}
return dp[m - 1, n - 1];
}
This code calculates how many unique ways there are to move from the top-left to the bottom-right of an m by n grid, moving only down or right.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Nested loops filling the dp table.
- How many times: The inner loop runs (m-1) * (n-1) times, covering almost all cells except the first row and column.
As the grid size grows, the number of operations grows roughly with the total number of cells.
| Input Size (m x n) | Approx. Operations |
|---|---|
| 10 x 10 | ~81 (9*9) |
| 100 x 100 | ~9,801 (99*99) |
| 1000 x 1000 | ~998,001 (999*999) |
Pattern observation: Doubling the grid size roughly quadruples the operations, showing a growth proportional to the product of m and n.
Time Complexity: O(m * n)
This means the time needed grows directly with the number of cells in the grid.
[X] Wrong: "Because there are two loops, the time complexity is O(m + n)."
[OK] Correct: The loops are nested, so the inner loop runs completely for each iteration of the outer loop, multiplying the counts, not adding.
Understanding this time complexity helps you explain how dynamic programming efficiently solves grid path problems, a common topic in interviews.
"What if we added obstacles that block some cells? How would the time complexity change?"
Practice
Solution
Step 1: Understand the problem goal
The problem asks for counting all possible paths from the top-left to bottom-right corner moving only right or down.Step 2: Differentiate from other path problems
It is not about shortest path or sums, but counting unique ways to reach the destination.Final Answer:
The number of different ways to move from the top-left to the bottom-right corner of a grid using only right and down moves. -> Option CQuick Check:
Unique paths count = number of ways [OK]
- Confusing unique paths with shortest path length
- Including diagonal moves
- Counting obstacles instead of paths
Solution
Step 1: Recall C# 2D array syntax
In C#, a rectangular 2D array is declared as int[,] dp = new int[m, n];Step 2: Identify incorrect syntax
Options B, C, and D misuse jagged array or rectangular array syntax.Final Answer:
int[,] dp = new int[m, n]; -> Option AQuick Check:
Rectangular 2D array uses [,] syntax [OK]
- Using jagged array syntax incorrectly
- Mixing rectangular and jagged array declarations
- Missing array size initialization
int m = 3, n = 3;
int[,] dp = new int[m, n];
for (int i = 0; i < m; i++) dp[i, 0] = 1;
for (int j = 0; j < n; j++) dp[0, j] = 1;
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
dp[i, j] = dp[i - 1, j] + dp[i, j - 1];
}
}
Console.WriteLine(dp[m - 1, n - 1]);Solution
Step 1: Initialize first row and column
First row and column are set to 1, representing only one path along edges.Step 2: Calculate dp values for inner cells
Each cell dp[i,j] = dp[i-1,j] + dp[i,j-1]. For 3x3 grid, dp[2,2] = 6.Final Answer:
6 -> Option DQuick Check:
Unique paths in 3x3 grid = 6 [OK]
- Not initializing first row and column to 1
- Adding instead of summing paths
- Off-by-one errors in loops
int m = 3, n = 3;
int[,] dp = new int[m, n];
for (int i = 0; i <= m; i++) dp[i, 0] = 1;
for (int j = 0; j <= n; j++) dp[0, j] = 1;
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
dp[i, j] = dp[i - 1, j] + dp[i, j - 1];
}
}
Console.WriteLine(dp[m - 1, n - 1]);Solution
Step 1: Check loop boundaries
Loops use i <= m and j <= n which exceed array bounds (max index m-1, n-1).Step 2: Identify runtime error
This causes IndexOutOfRangeException at runtime when accessing dp[m,0] or dp[0,n].Final Answer:
The loops use <= causing index out of range exception. -> Option BQuick Check:
Loop bounds must be less than array size [OK]
- Using <= instead of < in loops
- Ignoring array index limits
- Misunderstanding zero-based indexing
int[,] obstacleGrid = {
{0,0,0,0,0},
{0,1,0,1,0},
{0,0,0,0,0},
{1,0,1,0,0}
};
Which approach correctly adapts the DP solution?Solution
Step 1: Set dp to 0 where obstacles exist
For cells with obstacleGrid[i,j] == 1, dp[i,j] must be 0 to block paths.Step 2: Calculate dp with obstacle checks
For other cells, dp[i,j] = dp[i-1,j] + dp[i,j-1], ensuring no paths through obstacles. Initialize first row and column carefully to stop paths after obstacles.Final Answer:
Initialize dp[i,j] = 0 if obstacleGrid[i,j] == 1; else dp[i,j] = dp[i-1,j] + dp[i,j-1] (with boundary checks). Initialize first row and column carefully considering obstacles. -> Option AQuick Check:
Obstacles block paths, set dp=0 there [OK]
- Ignoring obstacles in dp calculation
- Not initializing first row/column correctly with obstacles
- Using recursion without memoization causing inefficiency
Ready to prove it?
Pass the test and this pattern moves unknown
