We use two pointers at the ends and track the maximum height seen from the left (leftMax) and the right (rightMax). The water trapped at any point is determined by the lower of the two maximums.
- Initialize
l = 0,r = n - 1,leftMax = height[0],rightMax = height[n-1],res = 0. - While
l < r:- If
leftMax < rightMax:l += 1.leftMax = max(leftMax, height[l]).res += leftMax - height[l].
- Else:
r -= 1.rightMax = max(rightMax, height[r]).res += rightMax - height[r].
- If
- Return
res.
- Time Complexity: O(N).
- Space Complexity: O(1).
def trap(height):
if not height: return 0
l, r = 0, len(height) - 1
leftMax, rightMax = height[l], height[r]
res = 0
while l < r:
if leftMax < rightMax:
l += 1
leftMax = max(leftMax, height[l])
res += leftMax - height[l]
else:
r -= 1
rightMax = max(rightMax, height[r])
res += rightMax - height[r]
return res