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

Frog Jump with K Distance visualizer

Learn Frog Jump with K Distance 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 / 10Execution step
Reference example · JavaScript
Inspect reference input
{
  "a": [
    10,
    30,
    40,
    50,
    20
  ],
  "k": 3
}

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = ({
a,k
},s)=>{
let d=[0];
for(let i=1;
i<a.length;
i++){
d[i]=Infinity;
for(let j=1;
j<=k&&j<=i;
j++){
d[i]=Math.min(d[i],d[i-j]+Math.abs(a[i]-a[i-j]));
s("Compare jump from predecessor",{
Heights:a,DP:d,Cell:[i,i-j]
})
}
}
return d.at(-1)
};

Execution canvas

Active Changed
STEP 1

Compare jump from predecessor

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

Heights5 items
DP2 items
Cell2 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

{
  "a": [
    10,
    30,
    40,
    50,
    20
  ],
  "k": 3
}

Execution steps

  1. Compare jump from predecessor

Computed output

30
JavaScript reference implementation
const solve = ({
  a,k
},s)=>{
  let d=[0];
  for(let i=1;
  i<a.length;
  i++){
    d[i]=Infinity;
    for(let j=1;
    j<=k&&j<=i;
    j++){
      d[i]=Math.min(d[i],d[i-j]+Math.abs(a[i]-a[i-j]));
      s("Compare jump from predecessor",{
        Heights:a,DP:d,Cell:[i,i-j]
      })
    }
  }
  return d.at(-1)
};

Continue learning

Practice related DSA questions in the same topic.