1. Blocked unknowns
Infinity prevents invalid directions from winning.
const dp = Array(cols).fill(Infinity)Typorithm · medium
Step 1 of 8 · intro card
Dynamic Programming
Minimum Path Sum chooses the cheaper path to each grid cell from top or left.
Each cell has a path cost.
The transition reads top and left because moves are right/down.
A one-row dp array stores top and current-left costs.
State: minimum cost to current cell.
Invalid left edge uses Infinity.
Transition: cell + min(top, left).
Space O(cols).
function minPathSum(grid) {
const rows = grid.length;
const cols = grid[0].length;
const dp = Array(cols).fill(Infinity);
dp[0] = 0;
for (let row = 0; row < rows; row++) {
for (let col = 0; col < cols; col++) {
dp[col] = grid[row][col] + Math.min(dp[col], col > 0 ? dp[col - 1] : Infinity);
}
}
return dp[cols - 1];
}Connect each part of the code to the concept before you type.
Infinity prevents invalid directions from winning.
const dp = Array(cols).fill(Infinity)The first cell adds to zero cost.
dp[0] = 0Current cost plus cheaper of top or left.
grid[row][col] + Math.min(dp[col], col > 0 ? dp[col - 1] : Infinity)Read the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.
Algorithm lab · interactive
InputCell costs [[1,3,1],[1,5,1],[4,2,1]]
Follow temporary values, local state and supporting structures at this step.
↗ The 9-cell working table above is itself auxiliary memory. Input values are shown separately.
Input · not auxiliary memory
Costs row 03 entries
Costs row 13 entries
Costs row 23 entries
Around this step
Small, reproducible examples · ← → step · Space play/pause · Home/End first/last state