0
0
DSA Cprogramming~10 mins

Minimum Path Sum in Grid in DSA C - 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 starting value.

DSA C
dp[0][0] = [1];
Drag options to blanks, or click blank then click option'
Agrid[0][0]
B0
C1
D-1
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].
2fill in blank
medium

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

Fix 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'
Adp[i][j]
Bdp[i-1][j-1]
Cdp[i][j-1]
Dgrid[i][j]
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.
4fill in blank
hard

Fill 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'
A1
B0
C2
Dm
Attempts:
3 left
💡 Hint
Common Mistakes
Starting loops from 0 which overwrites initial values.
Starting from 2 which skips some cells.
5fill in blank
hard

Fill 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'
A+
B-
Cn-1
Dm-1
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].