1def trap(height):
2 if not height: return 0
3 left, right = 0, len(height) - 1
4 left_max, right_max = height[left], height[right]
5 water = 0
6 while left < right:
7 if height[left] < height[right]:
8 left += 1
9 left_max = max(left_max, height[left])
10 water += left_max - height[left]
11 else:
12 right -= 1
13 right_max = max(right_max, height[right])
14 water += right_max - height[right]
15 return water