Workspace Unique Paths Ii
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Unique Paths II visualizer

Learn Unique Paths II 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 / 10Execution step
Reference example · JavaScript
Inspect reference input
[
  [
    0,
    0,
    0
  ],
  [
    0,
    1,
    0
  ],
  [
    0,
    0,
    0
  ]
]

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = (a,s)=>{
let d=a.map(r=>r.map(()=>0));
for(let i=0;
i<a.length;
i++)for(let j=0;
j<a[i].length;
j++){
d[i][j]=a[i][j]?0:i===0&&j===0?1:(d[i-1]?.[j]||0)+(d[i][j-1]||0);
s("Add paths from left and above",{
Grid:a,DP:d,Cell:[i,j]
})
}
return d.at(-1).at(-1)
};

Execution canvas

Active Changed
STEP 1

Add paths from left and above

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

[
  [
    0,
    0,
    0
  ],
  [
    0,
    1,
    0
  ],
  [
    0,
    0,
    0
  ]
]

Execution steps

  1. Add paths from left and above

Computed output

2
JavaScript reference implementation
const solve = (a,s)=>{
  let d=a.map(r=>r.map(()=>0));
  for(let i=0;
  i<a.length;
  i++)for(let j=0;
  j<a[i].length;
  j++){
    d[i][j]=a[i][j]?0:i===0&&j===0?1:(d[i-1]?.[j]||0)+(d[i][j-1]||0);
    s("Add paths from left and above",{
      Grid:a,DP:d,Cell:[i,j]
    })
  }
  return d.at(-1).at(-1)
};

Continue learning

Practice related DSA questions in the same topic.