Back to questionsDSA Visualizers: 388 Coding Questions Cherry Pickup II visualizer Learn Cherry Pickup II with a step-by-step Dynamic Programming visualizer, JavaScript source code and a worked example. Practice grid dp.
INTERACTIVE EXECUTION STUDIOLEARN BY DOING Predict. Execute. Understand. Follow the source code, inspect live state changes, and predict the next operation before revealing it.
1 / 5 Execution step
Prediction modePanel width Reference example · JavaScript
Inspect reference input [
[
3,
1,
1
],
[
2,
5,
1
],
[
1,
5,
5
],
[
2,
1,
1
]
] Code Visuals
JavaScript reference implementation · highlighted trace statement
1 const solve = (a,s)=>{
2 let n=a[0 ].length,d=Array.from ({
3 length:n
4 },()=>Array(n).fill(0 ));
5 for (let i=a.length-1 ;
6 i>=0 ;
7 i--){
8 let next=Array.from ({
9 length:n
10 },()=>Array(n).fill(0 ));
11 for (let j=0 ;
12 j<n;
13 j++)for (let k=0 ;
14 k<n;
15 k++){
16 let b=-Infinity;
17 for (let x=Math.max(0 ,j-1 );
18 x<=Math.min(n-1 ,j+1 );
19 x++)for (let y=Math.max(0 ,k-1 );
20 y<=Math.min(n-1 ,k+1 );
21 y++)b=Math.max(b,d[x][y]);
22 next[j][k]=a[i][j]+(j===k?0 :a[i][k])+b
23 }
24 s("Combine both robots' possible columns",{
25 Grid:a,DP:next,Row:[i]
26 });
27 d=next
28 }
29 return d[0 ][n-1 ]
30 };
Execution canvas Active ChangedSTEP 1 Combine both robots' possible columns Inspect the highlighted code and the state below. Click individual cells or nodes to see their values.
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 [
[
3,
1,
1
],
[
2,
5,
1
],
[
1,
5,
5
],
[
2,
1,
1
]
] Execution steps Combine both robots' possible columns Computed output 24 JavaScript reference implementation const solve = (a,s)=>{
let n=a[0].length,d=Array.from({
length:n
},()=>Array(n).fill(0));
for(let i=a.length-1;
i>=0;
i--){
let next=Array.from({
length:n
},()=>Array(n).fill(0));
for(let j=0;
j<n;
j++)for(let k=0;
k<n;
k++){
let b=-Infinity;
for(let x=Math.max(0,j-1);
x<=Math.min(n-1,j+1);
x++)for(let y=Math.max(0,k-1);
y<=Math.min(n-1,k+1);
y++)b=Math.max(b,d[x][y]);
next[j][k]=a[i][j]+(j===k?0:a[i][k])+b
}
s("Combine both robots' possible columns",{
Grid:a,DP:next,Row:[i]
});
d=next
}
return d[0][n-1]
};Continue learning Practice related DSA questions in the same topic.