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) since height[1] > height[0]
stack: [0,1]
water trapped: 0
Why: Current bar taller, so push to stack to find trapping later
Step 3: Index 2 (height 0) is less than height at stack top (1), push 2
stack: [0,1,2]
water trapped: 0
Why: Lower bar might trap water later, keep it in stack
Step 4: Index 3 (height 2) is taller than height at stack top (0 at index 2), pop 2 and calculate trapped water
stack after pop: [0,1]
Calculate distance=3-1-1=1, bounded height=min(2,1)-0=1
Water trapped += 1*1=1
Push index 3
stack: [0,1,3]
water trapped: 1
Why: Found right boundary taller than bottom, so water trapped between
Step 5: Index 4 (height 1) less than top height (2), push 4
stack: [0,1,3,4]
water trapped: 1
Why: Lower bar might trap water later
Step 6: Index 5 (height 0) less than top height (1), push 5
stack: [0,1,3,4,5]
water trapped: 1
Why: Keep lower bar for future trapping
Step 7: Index 6 (height 1) taller than top (0 at 5), pop 5 and calculate trapped water
stack after pop: [0,1,3,4]
Distance=6-4-1=1, bounded height=min(1,1)-0=1
Water trapped +=1*1=1+1=2
Push 6
stack: [0,1,3,4,6]
water trapped: 2
Why: Water trapped between bars at 4 and 6 over popped bar 5
Step 8: Index 7 (height 3) taller than top (1 at 6), pop 6 and calculate water
stack after pop: [0,1,3,4]
Distance=7-4-1=2, bounded height=min(3,1)-1=0
Water trapped += 0
Then pop 4 (height 1), distance=7-3-1=3, bounded height=min(3,2)-1=1
Water trapped += 3*1=2+3=5
Then pop 3 (height 2), distance=7-1-1=5, bounded height=min(3,1)-2=-1 (0)
Water trapped += 0
Push 7
stack: [0,1,7]
water trapped: 5
Why: Multiple pops to calculate trapped water with taller right boundary
Step 9: Index 8 (height 2) less than top (3), push 8
stack: [0,1,7,8]
water trapped: 5
Why: Lower bar might trap water later
Step 10: Index 9 (height 1) less than top (2), push 9
stack: [0,1,7,8,9]
water trapped: 5
Why: Keep for future trapping
Step 11: Index 10 (height 2) taller than top (1 at 9), pop 9 and calculate water
stack after pop: [0,1,7,8]
Distance=10-8-1=1, bounded height=min(2,2)-1=1
Water trapped +=1*1=5+1=6
Push 10
stack: [0,1,7,8,10]
water trapped: 6
Why: Water trapped between bars at 8 and 10 over popped bar 9
Step 12: Index 11 (height 1) less than top (2), push 11
stack: [0,1,7,8,10,11]
water trapped: 6
Why: Lower bar might trap water later
Result: Final stack: [0,1,7,8,10,11]
Total water trapped: 6 units