Discover how to find the cheapest path through a city grid without checking every street!
Why Minimum Path Sum in Grid in DSA Rust?
Imagine you are walking through a city grid from the top-left corner to the bottom-right corner. Each block has a cost to cross, like traffic or tolls. You want to find the cheapest path to reach your destination.
Trying every possible path manually is like checking every street and turn in the city. It takes too long and is easy to make mistakes, especially if the grid is large.
Using the Minimum Path Sum method, we calculate the cheapest cost step-by-step, building from the start to the end. This way, we avoid checking all paths and find the best route quickly and correctly.
fn min_path_sum(grid: &Vec<Vec<i32>>) -> i32 {
// Try all paths recursively (very slow)
fn dfs(row: usize, col: usize, grid: &Vec<Vec<i32>>) -> i32 {
if row == grid.len() - 1 && col == grid[0].len() - 1 {
return grid[row][col];
}
let mut right = i32::MAX;
let mut down = i32::MAX;
if col + 1 < grid[0].len() {
right = dfs(row, col + 1, grid);
}
if row + 1 < grid.len() {
down = dfs(row + 1, col, grid);
}
grid[row][col] + right.min(down)
}
dfs(0, 0, grid)
}fn min_path_sum(grid: &mut Vec<Vec<i32>>) -> i32 {
let rows = grid.len();
let cols = grid[0].len();
for row in 1..rows {
grid[row][0] += grid[row - 1][0];
}
for col in 1..cols {
grid[0][col] += grid[0][col - 1];
}
for row in 1..rows {
for col in 1..cols {
grid[row][col] += grid[row - 1][col].min(grid[row][col - 1]);
}
}
grid[rows - 1][cols - 1]
}This method lets you quickly find the cheapest path through any grid, saving time and avoiding mistakes.
Delivery drivers use this to find the fastest or cheapest route through city blocks with different traffic or toll costs.
Manual path checking is slow and error-prone.
Minimum Path Sum uses step-by-step calculation to find the cheapest path.
This approach is efficient and reliable for grid-based path problems.
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 a grid that has the smallest sum of all numbers along the path.Step 2: Clarify path rules
Movement is only allowed down or right at any point, so the path must be continuous and only move in these directions.Final Answer:
The path with the smallest sum of numbers from top-left to bottom-right -> Option DQuick Check:
Minimum path sum means smallest total sum [OK]
- Confusing minimum with maximum sum
- Counting number of paths instead of sum
- Ignoring movement restrictions (only right/down)
Solution
Step 1: Identify correct 2D vector syntax in Rust
Rust uses vec![vec![...], vec![...], ...] to create a 2D vector. let grid = vec![vec![1, 3, 1], vec![1, 5, 1], vec![4, 2, 1]]; uses this correct syntax.Step 2: Check other options for errors
let grid = [1, 3, 1, 1, 5, 1, 4, 2, 1]; is a 1D array, not 2D. let grid = vec![1, 3, 1; 1, 5, 1; 4, 2, 1]; uses invalid syntax with semicolons inside vec!. let grid = vec![[1, 3, 1], [1, 5, 1], [4, 2, 1]]; uses nested arrays inside vec! which is invalid.Final Answer:
let grid = vec![vec![1, 3, 1], vec![1, 5, 1], vec![4, 2, 1]]; -> Option AQuick Check:
vec![vec![...], vec![...]] creates 2D vector [OK]
- Using semicolons inside vec! macro
- Confusing 1D and 2D vector syntax
- Using arrays inside vec! without vec! wrapper
fn min_path_sum(grid: &mut Vec) -> i32 {
let m = grid.len();
let n = grid[0].len();
for i in 1..m {
grid[i][0] += grid[i - 1][0];
}
for j in 1..n {
grid[0][j] += grid[0][j - 1];
}
for i in 1..m {
for j in 1..n {
grid[i][j] += grid[i - 1][j].min(grid[i][j - 1]);
}
}
grid[m - 1][n - 1]
}
fn main() {
let mut grid = vec![
vec![1, 3, 1],
vec![1, 5, 1],
vec![4, 2, 1],
];
println!("{}", min_path_sum(&mut grid));
}Solution
Step 1: Calculate first column cumulative sums
grid[1][0] = 1 + 1 = 2, grid[2][0] = 4 + 2 = 6Step 2: Calculate first row cumulative sums
grid[0][1] = 3 + 1 = 4, grid[0][2] = 1 + 4 = 5Step 3: Calculate remaining cells
grid[1][1] = 5 + min(4,2) = 5 + 2 = 7, grid[1][2] = 1 + min(5,7) = 1 + 5 = 6, grid[2][1] = 2 + min(7,6) = 2 + 6 = 8, grid[2][2] = 1 + min(6,8) = 1 + 6 = 7Final Answer:
7 -> Option BQuick Check:
Minimum path sum = 7 [OK]
- Not updating first row and column sums correctly
- Adding max instead of min of neighbors
- Mixing indices in loops
fn min_path_sum(grid: &mut Vec) -> i32 {
let m = grid.len();
let n = grid[0].len();
for i in 1..m {
grid[i][0] = grid[i - 1][0]; // Bug here
}
for j in 1..n {
grid[0][j] += grid[0][j - 1];
}
for i in 1..m {
for j in 1..n {
grid[i][j] += grid[i - 1][j].min(grid[i][j - 1]);
}
}
grid[m - 1][n - 1]
}Solution
Step 1: Check first column update
The line grid[i][0] = grid[i - 1][0]; overwrites the current cell instead of adding the previous sum, so it loses the current cell's value.Step 2: Correct the update to accumulate sums
It should be grid[i][0] += grid[i - 1][0]; to add the previous cell's sum to the current cell's value.Final Answer:
Change grid[i][0] = grid[i - 1][0]; to grid[i][0] += grid[i - 1][0]; -> Option CQuick Check:
First column sums must accumulate [OK]
- Overwriting instead of adding in first column
- Changing correct addition in first row
- Using max instead of min for path sum
[[1, 3, 1, 2], [1, 5, 1, 1], [4, 2, 1, 3], [2, 1, 2, 1]]
What is the minimum path sum from top-left to bottom-right moving only right or down?
Solution
Step 1: Calculate first row and column cumulative sums
First row: [1, 4, 5, 7]
First column: [1, 2, 6, 8]Step 2: Calculate inner cells using min of top or left
Row 1: grid[1][1] = 5 + min(4,2) = 7
grid[1][2] = 1 + min(5,7) = 6
grid[1][3] = 1 + min(7,6) = 7
Row 2: grid[2][1] = 2 + min(7,6) = 8
grid[2][2] = 1 + min(6,8) = 7
grid[2][3] = 3 + min(7,7) = 10
Row 3: grid[3][1] = 1 + min(8,8) = 9
grid[3][2] = 2 + min(7,9) = 9
grid[3][3] = 1 + min(10,9) = 10Step 3: Final minimum path sum
The bottom-right cell value is 10, but we must check carefully:
Recalculate carefully:
First row: 1,4,5,7
First col: 1,2,6,8
grid[1][1] = 5 + min(4,2) = 5 + 2 = 7
grid[1][2] = 1 + min(5,7) = 1 + 5 = 6
grid[1][3] = 1 + min(7,6) = 1 + 6 = 7
grid[2][1] = 2 + min(7,6) = 2 + 6 = 8
grid[2][2] = 1 + min(6,8) = 1 + 6 = 7
grid[2][3] = 3 + min(7,7) = 3 + 7 = 10
grid[3][1] = 1 + min(8,8) = 1 + 8 = 9
grid[3][2] = 2 + min(7,9) = 2 + 7 = 9
grid[3][3] = 1 + min(10,9) = 1 + 9 = 10Final Answer:
10 -> Option AQuick Check:
Bottom-right cell after DP = 10 [OK]
- Miscomputing min between top and left cells
- Forgetting to update first row or column sums
- Adding max instead of min
Ready to prove it?
Pass the test and this pattern moves unknown
