📖 Scenario: You are helping a delivery robot find the cheapest path through a city grid. Each cell in the grid has a cost to enter. The robot can only move right or down. Your job is to find the path with the smallest total cost from the top-left corner to the bottom-right corner.
🎯 Goal: Build a Rust program that calculates the minimum path sum in a grid of costs. You will create the grid, set up a helper variable, write the logic to find the minimum path sum, and print the result.
📋 What You'll Learn
Create a 2D vector called grid with the exact values [[1,3,1],[1,5,1],[4,2,1]]
Create a variable called rows that stores the number of rows in grid
Use nested for loops with variables i and j to update grid with minimum path sums
Print the minimum path sum found at grid[rows - 1][cols - 1]
💡 Why This Matters
🌍 Real World
Finding the cheapest or fastest route in a grid-like map is common in delivery, robotics, and game development.
💼 Career
Understanding grid traversal and dynamic programming is important for software engineers working on pathfinding, optimization, and algorithm design.
Progress0 / 4 steps
1
Create the grid
Create a 2D vector called grid with these exact values: vec![vec![1, 3, 1], vec![1, 5, 1], vec![4, 2, 1]]
DSA Rust
Hint
Use let mut grid = vec![vec![1, 3, 1], vec![1, 5, 1], vec![4, 2, 1]]; to create the grid.
2
Set up row and column counts
Create a variable called rows that stores the number of rows in grid using grid.len(). Also create a variable called cols that stores the number of columns in grid using grid[0].len().
DSA Rust
Hint
Use let rows = grid.len(); and let cols = grid[0].len(); to get the sizes.
3
Calculate minimum path sums
Use nested for loops with variables i from 0 to rows and j from 0 to cols to update grid[i][j] with the minimum path sum. For the first row, add the left cell. For the first column, add the top cell. For others, add the minimum of top or left cells.
DSA Rust
Hint
Use nested loops and update each cell based on top and left neighbors.
4
Print the minimum path sum
Print the minimum path sum stored at grid[rows - 1][cols - 1] using println!("{}", grid[rows - 1][cols - 1]);.
DSA Rust
Hint
Use println!("{}", grid[rows - 1][cols - 1]); to print the result.
Practice
(1/5)
1. What does the "Minimum Path Sum in Grid" problem ask you to find? Given a grid of numbers, find the path from top-left to bottom-right with the smallest sum.
easy
A. The path with the largest sum of numbers from top-left to bottom-right
B. The maximum number in the grid
C. The number of paths from top-left to bottom-right
D. The path with the smallest sum of numbers from top-left to bottom-right
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 D
Quick Check:
Minimum path sum means smallest total sum [OK]
Hint: Look for smallest sum path from top-left to bottom-right [OK]
Common Mistakes:
Confusing minimum with maximum sum
Counting number of paths instead of sum
Ignoring movement restrictions (only right/down)
2. Which Rust code snippet correctly initializes a 2D vector grid with values for the Minimum Path Sum problem?
easy
A. let grid = vec![vec![1, 3, 1], vec![1, 5, 1], vec![4, 2, 1]];
B. let grid = [1, 3, 1, 1, 5, 1, 4, 2, 1];
C. let grid = vec![1, 3, 1; 1, 5, 1; 4, 2, 1];
D. let grid = vec![[1, 3, 1], [1, 5, 1], [4, 2, 1]];
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 A
Quick Check:
vec![vec![...], vec![...]] creates 2D vector [OK]
Hint: Use vec![vec![...], vec![...]] for 2D vectors in Rust [OK]
Common Mistakes:
Using semicolons inside vec! macro
Confusing 1D and 2D vector syntax
Using arrays inside vec! without vec! wrapper
3. What is the output of this Rust code that calculates the minimum path sum in a 3x3 grid?
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));
}
Hint: Add min of top or left cell to current cell [OK]
Common Mistakes:
Not updating first row and column sums correctly
Adding max instead of min of neighbors
Mixing indices in loops
4. Identify the bug in this Rust function for minimum path sum and select the fix:
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]
}
medium
A. Change grid[i][j] += grid[i - 1][j].min(grid[i][j - 1]); to grid[i][j] = grid[i - 1][j].max(grid[i][j - 1]);
B. Change grid[0][j] += grid[0][j - 1]; to grid[0][j] = grid[0][j - 1];
C. Change grid[i][0] = grid[i - 1][0]; to grid[i][0] += grid[i - 1][0];
D. No bug, code is correct
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 C
Quick Check:
First column sums must accumulate [OK]
Hint: Add previous cell value, don't overwrite it [OK]