Step 1: Push index 0 (height 0) onto stack
Stack: [0]
Water trapped: 0
Why: Start with first bar as potential boundary
Step 2: Push index 1 (height 1) onto stack since height[1] > height[0]
Stack: [0,1]
Water trapped: 0
Why: Current bar taller, so push to find right boundary later
Step 3: Push index 2 (height 0) onto stack since height[2] < height[1]
Stack: [0,1,2]
Water trapped: 0
Why: Lower bar may trap water later
Step 4: At index 3 (height 2), pop index 2 (height 0) from stack; calculate trapped water between index 1 and 3
Stack after pop: [0,1]
Calculate distance=3-1-1=1
Bounded height=min(2,1)-0=1
Water trapped += 1*1=1
Water trapped: 1
Why: Found right boundary taller than middle bar, so water trapped here
Step 5: Pop index 1 (height 1) since height[3]=2 > height[1]=1; calculate trapped water between index 0 and 3
Stack after pop: [0]
Distance=3-0-1=2
Bounded height=min(2,0)-1= -1 (negative, so 0)
Water trapped: 1
Why: No water trapped here because left boundary is lower
Step 6: Push index 3 (height 2) onto stack
Stack: [0,3]
Water trapped: 1
Why: Current bar becomes new boundary
Step 7: Push index 4 (height 1) onto stack since height[4]<height[3]
Stack: [0,3,4]
Water trapped: 1
Why: Lower bar may trap water later
Step 8: Push index 5 (height 0) onto stack since height[5]<height[4]
Stack: [0,3,4,5]
Water trapped: 1
Why: Lower bar may trap water later
Step 9: At index 6 (height 1), pop index 5 (height 0); calculate trapped water between index 4 and 6
Stack after pop: [0,3,4]
Distance=6-4-1=1
Bounded height=min(1,1)-0=1
Water trapped += 1*1=1
Water trapped: 2
Why: Water trapped in the valley formed by bars at 4 and 6
Step 10: Height[6]=1 equals height[4]=1, pop index 4 (height 1) and push index 6
Stack after pop: [0,3]
Push 6
Stack: [0,3,6]
Water trapped: 2
Why: Replace boundary with current bar of same height
Step 11: At index 7 (height 3), pop index 6 (height 1); calculate trapped water between index 3 and 7
Stack after pop: [0,3]
Distance=7-3-1=3
Bounded height=min(3,2)-1=1
Water trapped += 3*1=3
Water trapped: 5
Why: Water trapped in valley between bars at 3 and 7
Step 12: Pop index 3 (height 2) since height[7]=3 > height[3]=2; push index 7
Stack after pop: [0]
Push 7
Stack: [0,7]
Water trapped: 5
Why: Update boundary to taller bar
Step 13: Push index 8 (height 2) since height[8]<height[7]
Stack: [0,7,8]
Water trapped: 5
Why: Lower bar may trap water later
Step 14: Push index 9 (height 1) since height[9]<height[8]
Stack: [0,7,8,9]
Water trapped: 5
Why: Lower bar may trap water later
Step 15: At index 10 (height 2), pop index 9 (height 1); calculate trapped water between index 8 and 10
Stack after pop: [0,7,8]
Distance=10-8-1=1
Bounded height=min(2,2)-1=1
Water trapped += 1*1=1
Water trapped: 6
Why: Water trapped in valley between bars at 8 and 10
Step 16: Pop index 8 (height 2) since height[10]=2 equals height[8]=2; push index 10
Stack after pop: [0,7]
Push 10
Stack: [0,7,10]
Water trapped: 6
Why: Replace boundary with current bar of same height
Step 17: Push index 11 (height 1) since height[11]<height[10]
Stack: [0,7,10,11]
Water trapped: 6
Why: Lower bar may trap water later
Result: Stack: [0,7,10,11]
Total water trapped: 6