Minimum Path Sum in Grid in DSA C - 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.
int minPathSum(int** grid, int gridSize, int* gridColSize) {
int m = gridSize, n = gridColSize[0];
for (int i = 1; i < m; i++)
grid[i][0] += grid[i-1][0];
for (int j = 1; j < n; j++)
grid[0][j] += grid[0][j-1];
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
grid[i][j] += (grid[i-1][j] < grid[i][j-1]) ? grid[i-1][j] : grid[i][j-1];
}
}
return grid[m-1][n-1];
}
This code calculates the minimum 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 traversing the grid cells.
- How many times: Outer loop runs m-1 times, inner loop runs n-1 times, so roughly m x n times.
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 | About 100 operations |
| 100 x 100 | About 10,000 operations |
| 1000 x 1000 | About 1,000,000 operations |
Pattern observation: Doubling the grid size roughly quadruples the operations, showing growth proportional to the number of cells.
Time Complexity: O(m x n)
This means the time needed grows proportionally to the total number of cells in the grid.
[X] Wrong: "The time grows exponentially because there are many paths."
[OK] Correct: The code uses dynamic programming to avoid repeating calculations, so it only visits each cell once, not all paths.
Understanding this time complexity helps you explain how dynamic programming improves efficiency by avoiding repeated work, a key skill in problem solving.
"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. Which is true?Solution
Step 1: Understand the movement constraints
The problem allows moving only right or down from each cell in the grid.Step 2: Identify the goal
The goal is to find the path from the top-left corner to the bottom-right corner with the minimum sum of all numbers on the path.Final Answer:
Find the path with the smallest sum moving only right or down. -> Option AQuick Check:
Minimum path sum = smallest sum with right/down moves [OK]
- Thinking diagonal moves are allowed
- Looking for maximum sum instead of minimum
- Moving left or up which is not allowed
grid with 3 rows and 3 columns filled with zeros?Solution
Step 1: Understand array initialization in C
Initializing a 2D array with zeros can be done by setting all elements to zero using memset after declaration.Step 2: Check each option
int grid[3][3]; memset(grid, 0, sizeof(grid)); declares the array and then uses memset to set all bytes to zero, correctly initializing all elements to zero.Final Answer:
int grid[3][3]; memset(grid, 0, sizeof(grid)); -> Option CQuick Check:
memset sets all elements to zero [OK]
- Using {1} only initializes first element
- Confusing single braces with double braces
- Not including string.h for memset
int grid[2][2] = {{1, 3}, {1, 5}};
int dp[2][2];
dp[0][0] = grid[0][0];
for(int i=1; i<2; i++) dp[0][i] = dp[0][i-1] + grid[0][i];
for(int i=1; i<2; i++) dp[i][0] = dp[i-1][0] + grid[i][0];
for(int i=1; i<2; i++) {
for(int j=1; j<2; j++) {
dp[i][j] = (dp[i-1][j] < dp[i][j-1] ? dp[i-1][j] : dp[i][j-1]) + grid[i][j];
}
}
printf("%d\n", dp[1][1]);Solution
Step 1: Calculate first row and column sums
dp[0][0] = 1
dp[0][1] = 1 + 3 = 4
dp[1][0] = 1 + 1 = 2Step 2: Calculate dp[1][1]
Minimum of dp[0][1] = 4 and dp[1][0] = 2 is 2
dp[1][1] = 2 + 5 = 7Final Answer:
7 -> Option DQuick Check:
Minimum path sum = 7 [OK]
- Adding max instead of min
- Incorrect dp initialization
- Off-by-one errors in loops
int grid[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int dp[3][3];
dp[0][0] = grid[0][0];
for(int i=1; i<3; i++) dp[0][i] = dp[0][i-1] + grid[0][i];
for(int i=1; i<3; i++) dp[i][0] = dp[i-1][0] + grid[i][0];
for(int i=1; i<3; i++) {
for(int j=1; j<3; j++) {
dp[i][j] = dp[i-1][j] + dp[i][j-1] + grid[i][j];
}
}Solution
Step 1: Analyze dp update formula
The code adds dp[i-1][j] and dp[i][j-1] together plus grid[i][j], which is incorrect.Step 2: Correct dp update
It should take the minimum of dp[i-1][j] and dp[i][j-1], then add grid[i][j].Final Answer:
The dp update adds both dp[i-1][j] and dp[i][j-1] instead of taking the minimum. -> Option AQuick Check:
Use min(dp[i-1][j], dp[i][j-1]) + grid[i][j] [OK]
- Adding dp values instead of min
- Wrong loop ranges
- Not initializing dp[0][0]
int grid[3][3] = {
{1, 3, 1},
{1, 5, 1},
{4, 2, 1}
};Write the minimum path sum from top-left to bottom-right moving only right or down. What is the minimum sum?
Solution
Step 1: Calculate dp first row and column
dp[0][0] = 1
dp[0][1] = 1+3=4
dp[0][2] = 4+1=5
dp[1][0] = 1+1=2
dp[2][0] = 2+4=6Step 2: Calculate inner dp cells
dp[1][1] = min(dp[0][1], dp[1][0]) + 5 = min(4,2)+5=7
dp[1][2] = min(dp[0][2], dp[1][1]) + 1 = min(5,7)+1=6
dp[2][1] = min(dp[1][1], dp[2][0]) + 2 = min(7,6)+2=8
dp[2][2] = min(dp[1][2], dp[2][1]) + 1 = min(6,8)+1=7Final Answer:
7 -> Option BQuick Check:
Minimum path sum = 7 [OK]
- Choosing wrong min between top and left
- Incorrect dp initialization
- Misreading grid values
Ready to prove it?
Pass the test and this pattern moves unknown
