Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the first cell of the dp grid with the grid's first cell value.
DSA Rust
let mut dp = vec![vec![0; grid[0].len()]; grid.len()]; dp[0][0] = [1];
Attempts:
3 left
💡 Hint
Common Mistakes
Setting dp[0][0] to 0 instead of grid[0][0]
Using dp[0][0] on the right side causing self-reference
✗ Incorrect
The minimum path sum at the starting cell is the value of the grid's first cell itself.
2fill in blank
mediumComplete the code to fill the first row of dp by accumulating sums from the left.
DSA Rust
for j in 1..grid[0].len() { dp[0][j] = dp[0][j - 1] + [1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using dp[0][j] on the right side causing incorrect accumulation
Using grid[j][0] which is from the first column, not row
✗ Incorrect
Each cell in the first row accumulates the sum from the left cell plus the current grid cell value.
3fill in blank
hardFix the error in the code to fill the first column of dp by accumulating sums from above.
DSA Rust
for i in 1..grid.len() { dp[i][0] = dp[i - 1][0] + [1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using dp[i][0] on the right side causing incorrect accumulation
Using grid[0][i] which is from the first row, not column
✗ Incorrect
Each cell in the first column accumulates the sum from the above cell plus the current grid cell value.
4fill in blank
hardFill both blanks to compute the minimum path sum for each cell inside the grid.
DSA Rust
for i in 1..grid.len() { for j in 1..grid[0].len() { dp[i][j] = grid[i][j] + dp[i - 1][j][1]dp[i][j - 1]; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of .min() causing incorrect sums
Using * or - which are not correct for minimum calculation
✗ Incorrect
We add the current grid cell value to the minimum of the top and left dp cells.
5fill in blank
hardFill all three blanks to return the minimum path sum from top-left to bottom-right.
DSA Rust
fn min_path_sum(grid: Vec<Vec<i32>>) -> i32 {
let mut dp = vec![vec![0; grid[0].len()]; grid.len()];
dp[0][0] = [1];
for j in 1..grid[0].len() {
dp[0][j] = dp[0][j - 1] + [2];
}
for i in 1..grid.len() {
dp[i][0] = dp[i - 1][0] + [3];
}
for i in 1..grid.len() {
for j in 1..grid[0].len() {
dp[i][j] = grid[i][j] + dp[i - 1][j].min(dp[i][j - 1]);
}
}
dp[grid.len() - 1][grid[0].len() - 1]
}Attempts:
3 left
💡 Hint
Common Mistakes
Using dp values on the right side instead of grid values
Mixing row and column indices incorrectly
✗ Incorrect
Initialize dp[0][0] with grid[0][0], fill first row with grid[0][j], and first column with grid[i][0].
