Workspace Frog Jump
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Frog Jump visualizer

Learn Frog Jump with a step-by-step Dynamic Programming visualizer, JavaScript source code and a worked example. Practice one dimensional 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 / 4Execution step
Reference example · JavaScript
Inspect reference input
[
  10,
  20,
  30,
  10
]

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = (a,s)=>{
let d=[0];
for(let i=1;
i<a.length;
i++){
d[i]=Math.min(d[i-1]+Math.abs(a[i]-a[i-1]),i>1?d[i-2]+Math.abs(a[i]-a[i-2]):Infinity);
s("Choose cheaper predecessor",{
Heights:a,DP:d
},[i])
}
return d.at(-1)
};

Execution canvas

Active Changed
STEP 1

Choose cheaper predecessor

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

Heights4 items
DP2 items
Keyboard: Space to play · Left / Right to stepExplore next problem

Worked example and algorithm walkthrough

Topic: Dynamic Programming · One dimensional 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

[
  10,
  20,
  30,
  10
]

Execution steps

  1. Choose cheaper predecessor

Computed output

20
JavaScript reference implementation
const solve = (a,s)=>{
  let d=[0];
  for(let i=1;
  i<a.length;
  i++){
    d[i]=Math.min(d[i-1]+Math.abs(a[i]-a[i-1]),i>1?d[i-2]+Math.abs(a[i]-a[i-2]):Infinity);
    s("Choose cheaper predecessor",{
      Heights:a,DP:d
    },[i])
  }
  return d.at(-1)
};

Continue learning

Practice related DSA questions in the same topic.