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 starting value.
DSA C
dp[0][0] = [1];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing dp[0][0] with 0 or 1 instead of grid[0][0].
Forgetting to initialize dp[0][0].
✗ Incorrect
The minimum path sum starts at the top-left cell, so dp[0][0] must 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 < n; j++) { dp[0][j] = dp[0][j-1] [1] grid[0][j]; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Using dp[0][j+1] instead of dp[0][j-1].
✗ Incorrect
The minimum path sum for the first row is the sum of all previous cells plus the current grid value.
3fill in blank
hardFix the error in the code to correctly compute the minimum path sum for each cell.
DSA C
dp[i][j] = grid[i][j] + (dp[i-1][j] < dp[i][j-1] ? dp[i-1][j] : [1]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using dp[i-1][j-1] which is diagonal and not allowed.
Using dp[i][j] which is the current cell and causes wrong calculation.
✗ Incorrect
We choose the smaller of the top or left cell's dp values to add to the current grid cell.
4fill in blank
hardFill both blanks to complete the nested loops that fill the dp array starting from row 1 and column 1.
DSA C
for (int i = [1]; i < m; i++) { for (int j = [2]; j < n; j++) { dp[i][j] = grid[i][j] + (dp[i-1][j] < dp[i][j-1] ? dp[i-1][j] : dp[i][j-1]); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Starting loops from 0 which overwrites initial values.
Starting from 2 which skips some cells.
✗ Incorrect
We start filling dp from row 1 and column 1 because row 0 and column 0 are already initialized.
5fill in blank
hardFill all three blanks to complete the function that returns the minimum path sum from top-left to bottom-right.
DSA C
int minPathSum(int** grid, int m, int n) {
int dp[m][n];
dp[0][0] = grid[0][0];
for (int i = 1; i < m; i++) dp[i][0] = dp[i-1][0] [1] grid[i][0];
for (int j = 1; j < n; j++) dp[0][j] = dp[0][j-1] [2] grid[0][j];
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
dp[i][j] = grid[i][j] + (dp[i-1][j] < dp[i][j-1] ? dp[i-1][j] : dp[i][j-1]);
}
}
return dp[m-1][[3]];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' in dp initialization.
Returning dp[m-1][m-1] instead of dp[m-1][n-1].
✗ Incorrect
We add values when filling first column and row, and return dp[m-1][n-1] as the minimum path sum.