Workspace Minimum Falling Path Sum
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Minimum Falling Path Sum visualizer

Learn Minimum Falling Path Sum with a step-by-step Dynamic Programming visualizer, JavaScript source code and a worked example. Practice grid dp.

INTERACTIVE EXECUTION STUDIO
LEARN BY DOING

Predict. Execute. Understand.

Follow the source code, inspect live state changes, and predict the next operation before revealing it.

1 / 7Execution step
Reference example · JavaScript
Inspect reference input
{
  "a": [
    [
      2,
      1,
      3
    ],
    [
      6,
      5,
      4
    ],
    [
      7,
      8,
      9
    ]
  ],
  "triangle": false
}

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = ({
a,triangle
},s)=>{
let d=a.map(r=>r.slice());
for(let i=a.length-2;
i>=0;
i--)for(let j=0;
j<a[i].length;
j++){
let candidates=triangle?[d[i+1][j],d[i+1][j+1]]:[d[i+1][j-1],d[i+1][j],d[i+1][j+1]];
d[i][j]=a[i][j]+Math.min(...candidates.filter(x=>x!==undefined));
s("Choose cheapest next cell",{
Grid:a,DP:d,Cell:[i,j]
})
}
return triangle?d[0][0]:Math.min(...d[0])
};

Execution canvas

Active Changed
STEP 1

Choose cheapest next cell

Inspect the highlighted code and the state below. Click individual cells or nodes to see their values.

Grid3 rows
DP3 rows
Cell2 items
Keyboard: Space to play · Left / Right to stepExplore next problem

Worked example and algorithm walkthrough

Topic: Dynamic Programming · Grid DP

Follow the computed execution on the reference input. Advance one step at a time, inspect the changing values and compare your prediction with the final output.

Example input

{
  "a": [
    [
      2,
      1,
      3
    ],
    [
      6,
      5,
      4
    ],
    [
      7,
      8,
      9
    ]
  ],
  "triangle": false
}

Execution steps

  1. Choose cheapest next cell

Computed output

13
JavaScript reference implementation
const solve = ({
  a,triangle
},s)=>{
  let d=a.map(r=>r.slice());
  for(let i=a.length-2;
  i>=0;
  i--)for(let j=0;
  j<a[i].length;
  j++){
    let candidates=triangle?[d[i+1][j],d[i+1][j+1]]:[d[i+1][j-1],d[i+1][j],d[i+1][j+1]];
    d[i][j]=a[i][j]+Math.min(...candidates.filter(x=>x!==undefined));
    s("Choose cheapest next cell",{
      Grid:a,DP:d,Cell:[i,j]
    })
  }
  return triangle?d[0][0]:Math.min(...d[0])
};

Continue learning

Practice related DSA questions in the same topic.