0
0
DSA Typescriptprogramming~10 mins

Minimum Path Sum in Grid in DSA Typescript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A<
B(
C{
D[
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses or curly braces instead of square brackets for indexing.
2fill in blank
medium

Complete 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'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
3fill in blank
hard

Fix 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'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or division instead of addition.
4fill in blank
hard

Fill 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'
Amin
Bmax
Cabs
Dround
Attempts:
3 left
💡 Hint
Common Mistakes
Using Math.max instead of Math.min.
Using wrong index for left neighbor.
5fill in blank
hard

Fill 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'
A[m - 1]
B[n - 1]
C[m - 1][n - 1]
D[0][0]
Attempts:
3 left
💡 Hint
Common Mistakes
Returning dp[m - 1] or dp[n - 1] alone instead of dp[m - 1][n - 1].