Minimum Path Sum in Grid in DSA Java - Time & Space Complexity
We want to understand 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.
public int minPathSum(int[][] grid) {
int m = grid.length;
int n = grid[0].length;
int[][] dp = new int[m][n];
dp[0][0] = grid[0][0];
for (int i = 1; i < m; i++) dp[i][0] = dp[i-1][0] + grid[i][0];
for (int j = 1; j < n; j++) dp[0][j] = dp[0][j-1] + grid[0][j];
for (int i = 1; i < m; i++) {
for (int 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 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 each cell of the grid once.
- How many times: Each cell is visited exactly once in the double loop, totaling m x n times.
As the grid size grows, the number of operations grows with the total number of cells.
| Input Size (m x n) | Approx. Operations |
|---|---|
| 10 x 10 | 100 |
| 100 x 100 | 10,000 |
| 1000 x 1000 | 1,000,000 |
Pattern observation: The operations grow roughly by the product of rows and columns, so doubling both dimensions quadruples the work.
Time Complexity: O(m x n)
This means the time needed grows proportionally to the total number of cells in the grid.
[X] Wrong: "Since we only move right or down, the time complexity is O(m + n)."
[OK] Correct: Although the path length is about m + n, the algorithm must fill the entire grid to find the minimum sums, visiting all m x n cells.
Understanding this time complexity helps you explain how dynamic programming efficiently solves grid path problems by avoiding repeated work.
"What if we allowed movement in all four directions? 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 cell to the bottom-right cell of a grid such that the sum of all numbers on the path is as small as possible.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 AQuick Check:
Minimum path sum means smallest total sum [OK]
- Thinking the path can move diagonally or up
- Looking for maximum sum instead of minimum
- Counting number of paths instead of sum
grid with 3 rows and 3 columns filled with zeros?Solution
Step 1: Understand Java 2D array syntax
In Java, a 2D array is declared asint[][] name = new int[rows][columns];. This creates a grid with specified rows and columns.Step 2: Check each option
int[][] grid = new int[3][3]; correctly declares a 3x3 int 2D array. Options A, C, and D have incorrect syntax or dimensions.Final Answer:
int[][] grid = new int[3][3]; -> Option BQuick Check:
Correct 2D array syntax = int[][] grid = new int[3][3]; [OK]
- Declaring 2D arrays with wrong brackets
- Initializing only one dimension
- Confusing 1D and 2D array syntax
int[][] grid = {{1, 3}, {1, 5}};
int m = grid.length;
int n = grid[0].length;
int[][] dp = new int[m][n];
dp[0][0] = grid[0][0];
for (int i = 1; i < m; i++) dp[i][0] = dp[i-1][0] + grid[i][0];
for (int j = 1; j < n; j++) dp[0][j] = dp[0][j-1] + grid[0][j];
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
dp[i][j] = Math.min(dp[i-1][j], dp[i][j-1]) + grid[i][j];
}
}
System.out.println(dp[m-1][n-1]);Solution
Step 1: Initialize dp array with first row and column sums
dp[0][0] = 1
dp[1][0] = dp[0][0] + grid[1][0] = 1 + 1 = 2
dp[0][1] = dp[0][0] + grid[0][1] = 1 + 3 = 4Step 2: Calculate dp[1][1]
dp[1][1] = min(dp[0][1], dp[1][0]) + grid[1][1] = min(4, 2) + 5 = 2 + 5 = 7Final Answer:
7 -> Option AQuick Check:
Minimum path sum = 7 [OK]
- Adding max instead of min
- Not initializing first row and column correctly
- Mixing indices in loops
int[][] grid = {{1,2,3},{4,5,6}};
int m = grid.length;
int n = grid[0].length;
int[][] dp = new int[m][n];
dp[0][0] = grid[0][0];
for (int i = 1; i < m; i++) {
dp[i][0] = dp[i-1][0] + grid[i][0];
}
for (int j = 1; j < n; j++) {
dp[0][j] = dp[0][j-1] + grid[0][j];
}
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]) + grid[i][j];
}
}Solution
Step 1: Check dp initialization
dp[0][0] is correctly initialized to grid[0][0]. Loops for first row and column are correct.Step 2: Identify the error in dp update
The inner loop uses Math.max instead of Math.min, which finds the maximum path sum, not minimum.Final Answer:
Using Math.max instead of Math.min in dp calculation -> Option DQuick Check:
Minimum path sum needs Math.min, not Math.max [OK]
- Using max instead of min in dp update
- Forgetting to initialize dp[0][0]
- Incorrect loop boundaries
int[][] grid = {
{1, 2, 5},
{3, 2, 1},
{4, 1, 1}
};What is the minimum path sum from top-left to bottom-right moving only right or down?
Solution
Step 1: Initialize dp first row and column sums
dp[0][0] = 1
dp[0][1] = 1 + 2 = 3
dp[0][2] = 3 + 5 = 8
dp[1][0] = 1 + 3 = 4
dp[2][0] = 4 + 4 = 8Step 2: Calculate dp values for inner cells
dp[1][1] = min(dp[0][1], dp[1][0]) + 2 = min(3,4) + 2 = 5
dp[1][2] = min(dp[0][2], dp[1][1]) + 1 = min(8,5) + 1 = 6
dp[2][1] = min(dp[1][1], dp[2][0]) + 1 = min(5,8) + 1 = 6
dp[2][2] = min(dp[1][2], dp[2][1]) + 1 = min(6,6) + 1 = 7Final Answer:
7 -> Option CQuick Check:
Minimum path sum = 7 [OK]
- Not updating dp correctly for inner cells
- Adding max instead of min
- Incorrect initialization of first row or column
Ready to prove it?
Pass the test and this pattern moves unknown
