Step 1: Push index 0 (height 0) onto stack
stack: [0]
water trapped: 0
Why: Start with first bar index to compare with next bars
Step 2: i=1 height[1]=1 > height[0]=0, pop index 0, stack empty - no water calculation, push index 1
stack: [1]
water trapped: 0
Why: Pop smaller bar, no left boundary so no trapped water, push current as new boundary
Step 3: Push index 2 (height 0) since 0 < height[1]=1
stack: [1,2]
water trapped: 0
Why: Lower bar, possible bottom of a dip
Step 4: i=3 height 2 > height[2]=0, pop index 2
stack after pop: [1]
Calculate: distance=3-1-1=1, bounded height=min(2,1)-0=1, water +=1
Total water: 1
Why: Found dip: bar1(1) and bar3(2) trap 1 unit above bar2(0)
Step 5: Still at i=3, height 2 > height[1]=1, pop index 1, stack now empty - break no calculation
stack: []
Total water: 1
Why: No left boundary for popped bar1, so no additional water
Step 6: Push index 3 (height 2) onto stack
stack: [3]
Total water: 1
Why: Current bar becomes new potential boundary
Step 7: Push index 4 (height 1) < height[3]=2
stack: [3,4]
Total water: 1
Why: Lower bar, possible dip bottom
Step 8: Push index 5 (height 0) < height[4]=1
stack: [3,4,5]
Total water: 1
Why: Even lower, extends dip
Step 9: i=6 height 1 > height[5]=0, pop 5
stack: [3,4]
distance=6-4-1=1, bh=min(1,1)-0=1, water+=1 Total:2
Why: Trap 1 unit between bar4(1) and bar6(1) above bar5(0)
Step 10: At i=6 height1 == height[4]=1, no pop, push 6
stack: [3,4,6]
Total:2
Why: Equal height, push as potential boundary
Step 11: i=7 height3 > height[6]=1, pop 6
stack: [3,4]
dist=7-4-1=2, bh=min(3,1)-1=0, no water added Total:2
Why: Plateau between 4 and6 at height1, no trap
Step 12: Still i=7 3 > height[4]=1, pop 4
stack: [3]
dist=7-3-1=3, bh=min(3,2)-1=1, water+=3 Total:5
Why: Trap 1 height over 3 width between bar3(2) and bar7(3)
Step 13: Still i=7 3 > height[3]=2, pop 3, stack empty break no calc
stack: []
Total:5
Why: No left boundary for bar3
Step 14: Push current index 7 onto stack
stack: [7]
Total water: 5
Why: Set new boundary for future trapping
Step 15: Push index 8 (height 2) onto stack since height[8] < height[7]
stack: [7,8]
Total water: 5
Why: Lower bar, possible dip bottom
Step 16: Push index 9 (height 1) onto stack since height[9] < height[8]
stack: [7,8,9]
Total water: 5
Why: Lower bar, possible dip bottom
Step 17: Current bar index 10 (height 2) taller than top of stack (height 1 at index 9), pop index 9
stack after pop: [7,8]
Calculate trapped water:
Distance = 10 - 8 - 1 = 1
Bounded height = min(height[10], height[8]) - height[9] = min(2,2)-1=1
Water trapped += 1*1=1
Total water: 6
Why: Found dip bounded by bars at index 8 and 10 trapping 1 unit water
Step 18: Current bar height 2 equals height at top of stack (height 2 at index 8), push index 10
stack: [7,8,10]
Total water: 6
Why: Push to keep track of current bar
Step 19: Push index 11 (height 1) onto stack since height[11] < height[10]
stack: [7,8,10,11]
Total water: 6
Why: Lower bar, possible dip bottom
Result: Final trapped water: 6 units