Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the first cell of the dp array with the grid's first cell value.
DSA C#
dp[0, 0] = [1];
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing dp[0, 0] with 0 instead of grid[0][0].
Using dp[0, 0] on the right side instead of grid[0][0].
✗ Incorrect
The minimum path sum starting point is the value of the first cell in the grid, so dp[0, 0] should be initialized with grid[0][0].
2fill in blank
mediumComplete the code to fill the first row of the dp array by accumulating sums from the left.
DSA C#
for (int j = 1; j < cols; j++) { dp[0, j] = dp[0, j - 1] + [1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Adding dp[0, j] instead of grid[0][j], causing incorrect accumulation.
Using 0 instead of the grid value.
✗ Incorrect
Each cell in the first row accumulates the sum from the left plus the current grid cell value, so we add grid[0][j].
3fill in blank
hardFix the error in the code to fill the dp array for the rest of the grid cells.
DSA C#
for (int i = 1; i < rows; i++) { for (int j = 1; j < cols; j++) { dp[i, j] = Math.Min(dp[i - 1, j], dp[i, j - 1]) + [1]; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Adding dp[i][j] instead of grid[i][j], causing infinite recursion or wrong values.
Adding 0 instead of the grid value.
✗ Incorrect
To find the minimum path sum at dp[i, j], add the current grid cell value grid[i][j] to the minimum of the top and left dp cells.
4fill in blank
hardFill both blanks to initialize the first column of the dp array by accumulating sums from above.
DSA C#
for (int i = 1; i < rows; i++) { dp[i, 0] = dp[i - [1], 0] + [2]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of 1 for the row offset.
Adding dp[i][0] instead of grid[i][0].
✗ Incorrect
To fill the first column, add the value from the cell above (dp[i - 1, 0]) plus the current grid cell value grid[i][0].
5fill in blank
hardFill all three blanks to complete the method that returns the minimum path sum from top-left to bottom-right.
DSA C#
public int MinPathSum(int[][] grid) {
int rows = grid.Length;
int cols = grid[0].Length;
int[,] dp = new int[rows, cols];
dp[0, 0] = [1];
for (int i = 1; i < rows; i++) {
dp[i, 0] = dp[i - 1, 0] + [2];
}
for (int j = 1; j < cols; j++) {
dp[0, j] = dp[0, j - 1] + [3];
}
for (int i = 1; i < rows; i++) {
for (int j = 1; j < cols; j++) {
dp[i, j] = Math.Min(dp[i - 1, j], dp[i, j - 1]) + grid[i][j];
}
}
return dp[rows - 1, cols - 1];
}Attempts:
3 left
💡 Hint
Common Mistakes
Using dp values instead of grid values for initialization.
Mixing up row and column indices.
✗ Incorrect
Initialize dp[0, 0] with grid[0][0]. For the first column, add grid[i][0]. For the first row, add grid[0][j]. This sets up the base cases for the dp array.
