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 it's taller than height at stack top (0)
Stack: [0,1]
Water trapped: 0
Why: We need a taller bar to form a boundary for trapping water
Step 3: Index 2 (height 0) is lower, push it onto stack
Stack: [0,1,2]
Water trapped: 0
Why: Lower bar might form a valley for future trapping
Step 4: Index 3 (height 2) is taller than top of stack (height 0 at index 2), pop index 2
Stack after pop: [0,1]
Calculate trapped water between index 1 and 3
Distance = 3 - 1 - 1 = 1
Bounded height = min(height[1], height[3]) - height[2] = min(1,2)-0=1
Water trapped += 1*1=1
Why: Found a valley bounded by bars at indices 1 and 3 trapping 1 unit water
Step 5: Index 3 (height 2) still taller than top of stack (height 1 at index 1), pop index 1
Stack after pop: [0]
Calculate trapped water between index 0 and 3
Distance = 3 - 0 - 1 = 2
Bounded height = min(height[0], height[3]) - height[1] = min(0,2)-1 = -1 (no water)
Why: No water trapped because left boundary is lower
Step 6: Push index 3 (height 2) onto stack
Stack: [0,3]
Water trapped: 1
Why: New taller bar becomes boundary for future trapping
Step 7: Push index 4 (height 1) since it's lower than top of stack (height 2)
Stack: [0,3,4]
Water trapped: 1
Why: Lower bar might trap water later
Step 8: Index 5 (height 0) lower, push onto stack
Stack: [0,3,4,5]
Water trapped: 1
Why: Keep track of valley bottom
Step 9: Index 6 (height 1) taller than top of stack (height 0 at index 5), pop index 5
Stack after pop: [0,3,4]
Distance = 6 - 4 - 1 = 1
Bounded height = min(height[4], height[6]) - height[5] = min(1,1)-0=1
Water trapped += 1*1=1 (total 2)
Why: Water trapped in small valley between indices 4 and 6
Step 10: Index 6 (height 1) not taller than top of stack (height 1 at index 4), push index 6
Stack: [0,3,4,6]
Water trapped: 2
Why: Equal height bars can form boundaries
Step 11: Index 7 (height 3) taller than top of stack (height 1 at index 6), pop index 6
Stack after pop: [0,3,4]
Distance = 7 - 4 - 1 = 2
Bounded height = min(height[4], height[7]) - height[6] = min(1,3)-1=0
No water trapped
Why: No water trapped because bounded height is zero
Step 12: Index 7 (height 3) taller than top of stack (height 1 at index 4), pop index 4
Stack after pop: [0,3]
Distance = 7 - 3 - 1 = 3
Bounded height = min(height[3], height[7]) - height[4] = min(2,3)-1=1
Water trapped += 3*1=3 (total 5)
Why: Water trapped in bigger valley between indices 3 and 7
Step 13: Index 7 (height 3) taller than top of stack (height 2 at index 3), pop index 3
Stack after pop: [0]
Distance = 7 - 0 - 1 = 6
Bounded height = min(height[0], height[7]) - height[3] = min(0,3)-2 = -2 (no water)
Why: No water trapped because left boundary is lower
Step 14: Push index 7 (height 3) onto stack
Stack: [0,7]
Water trapped: 5
Why: New tall bar becomes boundary
Step 15: Index 8 (height 2) lower, push onto stack
Stack: [0,7,8]
Water trapped: 5
Why: Lower bar might trap water later
Step 16: Index 9 (height 1) lower, push onto stack
Stack: [0,7,8,9]
Water trapped: 5
Why: Keep track of valley bottom
Step 17: Index 10 (height 2) taller than top of stack (height 1 at index 9), pop index 9
Stack after pop: [0,7,8]
Distance = 10 - 8 - 1 = 1
Bounded height = min(height[8], height[10]) - height[9] = min(2,2)-1=1
Water trapped += 1*1=1 (total 6)
Why: Water trapped in small valley between indices 8 and 10
Step 18: Index 10 (height 2) not taller than top of stack (height 2 at index 8), push index 10
Stack: [0,7,8,10]
Water trapped: 6
Why: Equal height bars can form boundaries
Step 19: Index 11 (height 1) lower, push onto stack
Stack: [0,7,8,10,11]
Water trapped: 6
Why: Lower bar might trap water later
Result: Final trapped water = 6 units