Back to questionsDSA Visualizers: 388 Coding Questions Trapping Rain Water visualizer Learn Trapping Rain Water with a step-by-step Stacks & Queues visualizer, JavaScript source code and a worked example. Practice monotonic structures.
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 / 13 Execution step
Prediction modePanel width Reference example · JavaScript
Inspect reference input [
0,
1,
0,
2,
1,
0,
1,
3,
2,
1,
2,
1
] Code Visuals
JavaScript reference implementation · highlighted trace statement
1 const solve = (a,s)=>{
2 let l=0 ,r=a.length-1 ,lm=0 ,rm=0 ,w=0 ;
3 while (l<=r){
4 if (a[l]<=a[r]){
5 lm=Math.max(lm,a[l]);
6 w+=lm-a[l++]
7 }else {
8 rm=Math.max(rm,a[r]);
9 w+=rm-a[r--]
10 }
11 s("Process lower boundary",{
12 Heights:a,Bounds:[l,r],Maxima:[lm,rm],Water:[w]
13 })
14 }
15 return w
16 };
Execution canvas Active ChangedSTEP 1 Process lower boundary Inspect the highlighted code and the state below. Click individual cells or nodes to see their values.
0 0 1 1 0 2 2 3 1 4 0 5 1 6 3 7 2 8 1 9 2 10 1 11
Worked example and algorithm walkthrough Topic: Stacks & Queues · Monotonic structures
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,
1,
0,
2,
1,
0,
1,
3,
2,
1,
2,
1
] Execution steps Process lower boundary Computed output 6 JavaScript reference implementation const solve = (a,s)=>{
let l=0,r=a.length-1,lm=0,rm=0,w=0;
while(l<=r){
if(a[l]<=a[r]){
lm=Math.max(lm,a[l]);
w+=lm-a[l++]
}else{
rm=Math.max(rm,a[r]);
w+=rm-a[r--]
}
s("Process lower boundary",{
Heights:a,Bounds:[l,r],Maxima:[lm,rm],Water:[w]
})
}
return w
};Continue learning Practice related DSA questions in the same topic.