Minimum Path Sum in Grid in DSA Typescript - Time & Space Complexity
We want to know how the time needed to find the minimum path sum in a grid changes as the grid size grows.
How does the number of steps grow when the grid gets bigger?
Analyze the time complexity of the following code snippet.
function minPathSum(grid: number[][]): number {
const m = grid.length;
const n = grid[0].length;
const dp: number[][] = Array.from({ length: m }, () => Array(n).fill(0));
dp[0][0] = grid[0][0];
for (let i = 1; i < m; i++) dp[i][0] = dp[i - 1][0] + grid[i][0];
for (let j = 1; j < n; j++) dp[0][j] = dp[0][j - 1] + grid[0][j];
for (let i = 1; i < m; i++) {
for (let j = 1; j < n; j++) {
dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j];
}
}
return dp[m - 1][n - 1];
}
This code finds the smallest sum path from the top-left to the bottom-right of a grid, moving only down or right.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Nested loops that fill the dp table for each cell in the grid.
- How many times: The inner loop runs for each cell except the first row and column, so roughly m x n times.
As the grid size grows, the number of operations grows with the number of cells.
| Input Size (m x n) | Approx. Operations |
|---|---|
| 10 x 10 | About 100 operations |
| 100 x 100 | About 10,000 operations |
| 1000 x 1000 | About 1,000,000 operations |
Pattern observation: Operations grow roughly proportional to the total number of cells, so doubling rows and columns quadruples work.
Time Complexity: O(m x n)
This means the time needed grows proportionally with the total 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 each cell is processed once, making the total work multiply, not add.
Understanding this time complexity helps you explain how dynamic programming efficiently solves grid path problems, a common interview topic.
"What if we allowed moving diagonally as well? How would the time complexity change?"
Practice
Given a grid of numbers, find the path from top-left to bottom-right with the smallest sum.Solution
Step 1: Understand the problem goal
The problem asks to find a path from the top-left corner to the bottom-right corner of the grid that has the smallest sum of all numbers along the path.Step 2: Clarify path movement
Movement is only allowed down or right at each step, so the path is constrained.Final Answer:
The path with the smallest sum of numbers from top-left to bottom-right -> Option BQuick Check:
Minimum path sum means smallest total sum [OK]
- Confusing minimum with maximum sum
- Counting number of paths instead of sum
- Ignoring movement constraints (only right/down)
grid with values [[1,2],[3,4]]?Solution
Step 1: Understand 2D array initialization
To create a 2D array with specific values, use nested arrays like [[1,2],[3,4]].Step 2: Check each option
const grid = [[1,2],[3,4]]; correctly uses nested arrays. Options B and D create arrays but not nested with those values. const grid = [1,2,3,4]; is a flat array.Final Answer:
const grid = [[1,2],[3,4]]; -> Option CQuick Check:
Nested arrays create 2D arrays [OK]
- Using flat arrays instead of nested arrays
- Using Array constructor incorrectly
- Confusing array length with array content
const grid = [ [1, 3, 1], [1, 5, 1], [4, 2, 1] ];
Solution
Step 1: Trace possible paths and sums
Possible minimum path: 1 -> 3 -> 1 -> 1 -> 1 = 7 (wrong path, check carefully). Another path: 1 -> 1 -> 5 -> 1 -> 1 = 9. Correct minimum path is 1 -> 3 -> 1 -> 1 -> 1 = 7 is invalid because movement is only right/down. Let's check carefully:
Path: (0,0)=1 -> (1,0)=1 -> (2,0)=4 -> (2,1)=2 -> (2,2)=1 sum=1+1+4+2+1=9
Path: (0,0)=1 -> (0,1)=3 -> (0,2)=1 -> (1,2)=1 -> (2,2)=1 sum=1+3+1+1+1=7
But movement only right/down, so path is valid.
Check if any smaller sum exists.Step 2: Confirm minimum sum
The minimum path sum is 8 by moving right, down, down, right.
Path: (0,0)=1 -> (0,1)=3 -> (1,1)=5 -> (2,1)=2 -> (2,2)=1 sum=1+3+5+2+1=12 (too large)
Try path: (0,0)=1 -> (1,0)=1 -> (1,1)=5 -> (1,2)=1 -> (2,2)=1 sum=1+1+5+1+1=9
Try path: (0,0)=1 -> (1,0)=1 -> (2,0)=4 -> (2,1)=2 -> (2,2)=1 sum=1+1+4+2+1=9
Try path: (0,0)=1 -> (0,1)=3 -> (0,2)=1 -> (1,2)=1 -> (2,2)=1 sum=1+3+1+1+1=7 (valid)
So minimum sum is 7.Final Answer:
7 -> Option AQuick Check:
Sum of path 1->3->1->1->1 = 7 [OK]
- Adding numbers from invalid paths (not only right/down)
- Forgetting to include starting cell value
- Mixing row and column indices
function minPathSum(grid: number[][]): number {
const m = grid.length;
const n = grid[0].length;
const dp = Array(m).fill(Array(n).fill(0));
dp[0][0] = grid[0][0];
for (let i = 1; i < m; i++) dp[i][0] = dp[i-1][0] + grid[i][0];
for (let j = 1; j < n; j++) dp[0][j] = dp[0][j-1] + grid[0][j];
for (let i = 1; i < m; i++) {
for (let j = 1; j < n; j++) {
dp[i][j] = Math.min(dp[i-1][j], dp[i][j-1]) + grid[i][j];
}
}
return dp[m-1][n-1];
}Solution
Step 1: Analyze dp array initialization
Using Array(m).fill(Array(n).fill(0)) creates m references to the same inner array, so updating one row updates all rows.Step 2: Understand impact on dp updates
Because all rows share the same array, dp updates overwrite each other, causing wrong results.Final Answer:
dp array initialization shares references causing incorrect updates -> Option DQuick Check:
Array.fill with objects shares references [OK]
- Using fill for 2D array causes shared references
- Ignoring empty grid input checks
- Incorrect loop start indices
const grid = [ [2, 1, 3], [6, 5, 4], [7, 8, 9] ];
Write a TypeScript function to find the minimum path sum from top-left to bottom-right, moving only right or down. What is the minimum path sum?
Solution
Step 1: Calculate dp values row-wise and column-wise
Start dp[0][0] = 2.
First row: dp[0][1] = 2+1=3, dp[0][2] = 3+3=6.
First column: dp[1][0] = 2+6=8, dp[2][0] = 8+7=15.Step 2: Fill remaining dp cells using min of top or left + current grid value
dp[1][1] = min(dp[0][1], dp[1][0]) + 5 = min(3,8)+5=8
dp[1][2] = min(dp[0][2], dp[1][1]) + 4 = min(6,8)+4=10
dp[2][1] = min(dp[1][1], dp[2][0]) + 8 = min(8,15)+8=16
dp[2][2] = min(dp[1][2], dp[2][1]) + 9 = min(10,16)+9=19Final Answer:
19 -> Option AQuick Check:
Minimum path sum dp[m-1][n-1] = 19 [OK]
- Adding values incorrectly in dp table
- Mixing up indices for rows and columns
- Not considering only right/down moves
Ready to prove it?
Pass the test and this pattern moves unknown
