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 Typescript
const m = grid.length; const n = grid[0].length; const dp: number[][] = Array.from({ length: m }, () => Array(n).fill(0)); dp[0][0] = grid[1][0][0];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses or curly braces instead of square brackets for indexing.
✗ Incorrect
We use square brackets to access elements in a 2D array. So grid[0][0] accesses the first cell.
2fill in blank
mediumComplete the code to fill the first row of the dp array by accumulating sums from the left.
DSA Typescript
for (let 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.
✗ Incorrect
We add the previous sum dp[0][j - 1] to the current grid value to accumulate the path sum.
3fill in blank
hardFix the error in the code to correctly fill the first column of the dp array by accumulating sums from above.
DSA Typescript
for (let i = 1; i < m; i++) { dp[i][0] = dp[i - 1][0] [1] grid[i][0]; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or division instead of addition.
✗ Incorrect
We add the value from the cell above dp[i - 1][0] to the current grid value to accumulate the path sum.
4fill in blank
hardFill both blanks to compute the minimum path sum for each cell by choosing the smaller sum from top or left.
DSA Typescript
for (let i = 1; i < m; i++) { for (let j = 1; j < n; j++) { dp[i][j] = grid[i][j] + Math.[1](dp[i - 1][j], dp[i][[2] - 1]); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Math.max instead of Math.min.
Using wrong index for left neighbor.
✗ Incorrect
We use Math.min to pick the smaller sum from the top or left cell. The second blank is 'j' to access the left cell dp[i][j - 1].
5fill in blank
hardFill all three blanks to return the minimum path sum from top-left to bottom-right of the grid.
DSA Typescript
function minPathSum(grid: number[][]): number {
const m = grid.length;
const n = grid[0].length;
const dp: number[][] = Array.from({ length: m }, () => Array(n).fill(0));
dp[0][0] = grid[0][0];
for (let i = 1; i < m; i++) {
dp[i][0] = dp[i - 1][0] + grid[i][0];
}
for (let j = 1; j < n; j++) {
dp[0][j] = dp[0][j - 1] + grid[0][j];
}
for (let i = 1; i < m; i++) {
for (let j = 1; j < n; j++) {
dp[i][j] = grid[i][j] + Math.min(dp[i - 1][j], dp[i][j - 1]);
}
}
return dp[1][2][3];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning dp[m - 1] or dp[n - 1] alone instead of dp[m - 1][n - 1].
✗ Incorrect
We return dp[m - 1][n - 1], the bottom-right cell, which holds the minimum path sum.