Step 1: push index 0 (height 0) onto stack
stack: [0]
water trapped: 0
Why: Start with first bar to compare with next bars
Step 2: push index 1 (height 1) onto stack since height[1] > height[0]
stack: [0,1]
water trapped: 0
Why: Current bar is taller, so push to stack to find potential right boundary
Step 3: push index 2 (height 0) because next bar height[2]=0 is less
stack: [0,1,2]
water trapped: 0
Why: Current bar is lower, so push to stack to find dips
Step 4: pop index 2 (height 0) because height[3]=2 > height[2], calculate trapped water
stack: [0,1]
water trapped: 1 (between index 1 and 3)
Why: Found right boundary taller than bottom, calculate water trapped in between
Step 5: pop index 1 (height 1) because height[3]=2 > height[1], calculate trapped water
stack: [0]
water trapped: 2 (additional water between index 0 and 3)
Why: Found right boundary taller than bottom, calculate trapped water
Step 6: push index 3 (height 2) onto stack
stack: [0,3]
water trapped: 2
Why: Push new taller bar as potential right boundary
Step 7: push index 4 (height 1) onto stack since height[4] < height[3]
stack: [0,3,4]
water trapped: 2
Why: Lower bar, push to stack to find dips
Step 8: push index 5 (height 0) because height[5]=0 is lower
stack: [0,3,4,5]
water trapped: 2
Why: Current bar lower, push to stack to track dips
Step 9: pop index 5 (height 0) because height[6]=1 > height[5], calculate trapped water
stack: [0,3,4]
water trapped: 3 (additional water between index 5 and 6)
Why: Found right boundary taller than bottom, calculate trapped water
Step 10: pop index 4 (height 1) because height[6]=1 >= height[4], calculate trapped water
stack: [0,3]
water trapped: 3
Why: Right boundary taller or equal, calculate trapped water (no additional water here)
Step 11: push index 6 (height 1) onto stack
stack: [0,3,6]
water trapped: 3
Why: Push new bar as potential boundary
Step 12: pop index 6 (height 1) because height[7]=3 > height[6], calculate trapped water
stack: [0,3]
water trapped: 6 (water trapped between index 6 and 7)
Why: Right boundary taller, calculate trapped water
Step 13: pop index 3 (height 2) because height[7]=3 > height[3], calculate trapped water
stack: [0]
water trapped: 8 (additional water between index 3 and 7)
Why: Right boundary taller, calculate trapped water
Step 14: push index 7 (height 3) onto stack
stack: [0,7]
water trapped: 8
Why: Push new tallest bar
Step 15: push index 8 (height 2) onto stack since height[8] < height[7]
stack: [0,7,8]
water trapped: 8
Why: Lower bar, push to stack
Step 16: push index 9 (height 1) onto stack since height[9] < height[8]
stack: [0,7,8,9]
water trapped: 8
Why: Lower bar, push to stack
Step 17: pop index 9 (height 1) because height[10]=2 > height[9], calculate trapped water
stack: [0,7,8]
water trapped: 9 (water trapped between index 9 and 10)
Why: Right boundary taller, calculate trapped water
Step 18: pop index 8 (height 2) because height[10]=2 >= height[8], calculate trapped water
stack: [0,7]
water trapped: 9
Why: Right boundary taller or equal, calculate trapped water (no additional water here)
Step 19: push index 10 (height 2) onto stack
stack: [0,7,10]
water trapped: 9
Why: Push new bar as potential boundary
Step 20: push index 11 (height 1) onto stack since height[11] < height[10]
stack: [0,7,10,11]
water trapped: 9
Why: Lower bar, push to stack
Result: stack: [0,7,10,11]
water trapped: 9