Challenge - 5 Problems
Minimum Path Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Minimum Path Sum Calculation
What is the output of the following Rust code that calculates the minimum path sum in a 3x3 grid?
DSA Rust
fn min_path_sum(grid: Vec<Vec<i32>>) -> i32 {
let m = grid.len();
let n = grid[0].len();
let mut dp = vec![vec![0; n]; m];
dp[0][0] = grid[0][0];
for i in 1..m {
dp[i][0] = dp[i - 1][0] + grid[i][0];
}
for j in 1..n {
dp[0][j] = dp[0][j - 1] + grid[0][j];
}
for i in 1..m {
for j in 1..n {
dp[i][j] = grid[i][j] + dp[i - 1][j].min(dp[i][j - 1]);
}
}
dp[m - 1][n - 1]
}
fn main() {
let grid = vec![
vec![1, 3, 1],
vec![1, 5, 1],
vec![4, 2, 1],
];
println!("{}", min_path_sum(grid));
}Attempts:
2 left
💡 Hint
Trace the path with the smallest sum from top-left to bottom-right moving only down or right.
✗ Incorrect
The minimum path sum is calculated by dynamic programming. The path is 1 -> 3 -> 1 -> 1 -> 1 with sum 7.
❓ Predict Output
intermediateMinimum Path Sum for a 2x2 Grid
What is the output of this Rust function for the given 2x2 grid?
DSA Rust
fn min_path_sum(grid: Vec<Vec<i32>>) -> i32 {
let m = grid.len();
let n = grid[0].len();
let mut dp = vec![vec![0; n]; m];
dp[0][0] = grid[0][0];
for i in 1..m {
dp[i][0] = dp[i - 1][0] + grid[i][0];
}
for j in 1..n {
dp[0][j] = dp[0][j - 1] + grid[0][j];
}
for i in 1..m {
for j in 1..n {
dp[i][j] = grid[i][j] + dp[i - 1][j].min(dp[i][j - 1]);
}
}
dp[m - 1][n - 1]
}
fn main() {
let grid = vec![
vec![1, 2],
vec![1, 1],
];
println!("{}", min_path_sum(grid));
}Attempts:
2 left
💡 Hint
Consider the path 1 -> 1 -> 1 or 1 -> 2 -> 1.
✗ Incorrect
The minimum path sum is 3 by going down then right or right then down.
🔧 Debug
advancedIdentify the Runtime Error in Minimum Path Sum Code
What error does the following Rust code produce when run with a non-rectangular grid?
DSA Rust
fn min_path_sum(grid: Vec<Vec<i32>>) -> i32 {
let m = grid.len();
let n = grid[0].len();
let mut dp = vec![vec![0; n]; m];
dp[0][0] = grid[0][0];
for i in 1..m {
dp[i][0] = dp[i - 1][0] + grid[i][0];
}
for j in 1..n {
dp[0][j] = dp[0][j - 1] + grid[0][j];
}
for i in 1..m {
for j in 1..n {
dp[i][j] = grid[i][j] + dp[i - 1][j].min(dp[i][j - 1]);
}
}
dp[m - 1][n - 1]
}
fn main() {
let grid = vec![
vec![1, 3, 1],
vec![1, 5],
vec![4, 2, 1],
];
println!("{}", min_path_sum(grid));
}Attempts:
2 left
💡 Hint
Check if all rows have the same length before accessing grid[i][j].
✗ Incorrect
The grid rows have different lengths, so accessing grid[1][2] causes an index out of bounds panic at runtime.
🧠 Conceptual
advancedWhy Use Dynamic Programming for Minimum Path Sum?
Why is dynamic programming the best approach to solve the minimum path sum problem in a grid?
Attempts:
2 left
💡 Hint
Think about how the problem can be divided into smaller parts and how to avoid repeated work.
✗ Incorrect
Dynamic programming solves the problem by computing minimum sums for smaller subgrids and reusing those results to build the solution for the full grid efficiently.
❓ Predict Output
expertOutput of Minimum Path Sum with Negative Values
What is the output of this Rust code that calculates the minimum path sum in a grid containing negative values?
DSA Rust
fn min_path_sum(grid: Vec<Vec<i32>>) -> i32 {
let m = grid.len();
let n = grid[0].len();
let mut dp = vec![vec![0; n]; m];
dp[0][0] = grid[0][0];
for i in 1..m {
dp[i][0] = dp[i - 1][0] + grid[i][0];
}
for j in 1..n {
dp[0][j] = dp[0][j - 1] + grid[0][j];
}
for i in 1..m {
for j in 1..n {
dp[i][j] = grid[i][j] + dp[i - 1][j].min(dp[i][j - 1]);
}
}
dp[m - 1][n - 1]
}
fn main() {
let grid = vec![
vec![1, -3, 2],
vec![2, 5, -1],
vec![4, -2, 1],
];
println!("{}", min_path_sum(grid));
}Attempts:
2 left
💡 Hint
Consider paths that include negative values to minimize the sum.
✗ Incorrect
The minimum path sum is 0 by the path 1 -> -3 -> 2 -> -1 -> 1 with sum 0.
