Maximum Points You Can Obtain from Cards visualizer
Learn Maximum Points You Can Obtain from Cards with a step-by-step Sliding Window & Two Pointers visualizer, JavaScript source code and a worked example. Practice fixed and variable windows.
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.
Topic: Sliding Window & Two Pointers · Fixed and variable windows
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": [
1,
2,
3,
4,
5,
6,
1
],
"k": 3
}
Execution steps
Trade left card for right card
Computed output
12
JavaScript reference implementation
const solve = ({
a,k
},s)=>{
let v=a.slice(0,k).reduce((x,y)=>x+y,0),b=v;
for(let i=0;
i<k;
i++){
v+=a[a.length-1-i]-a[k-1-i];
b=Math.max(b,v);
s("Trade left card for right card",{
Array:a,Score:[v],Best:[b]
},[k-1-i,a.length-1-i])
}
return b
};