Open fockspaces opened 9 months ago
O(1) solution, too tricky so I'll not diving too deep into it.
The height of water is only determined by the shorter side, so we can update the ans with two pointers, keep operate on current cell, then moving toward by the shorter side
class Solution:
def trap(self, height: List[int]) -> int:
n = len(height)
l, r = 0, n - 1
left_h, right_h = height[l], height[r]
ans = 0
while l < r:
if left_h <= right_h:
l += 1
left_h = max(left_h, height[l])
ans += left_h - height[l]
else:
r -= 1
right_h = max(right_h, height[r])
ans += right_h - height[r]
return ans
the containing water: (the minimun height of wall from left and right side) - cur_wall height if the value is negative, means the cur_water is higer than the either side, water will flow through that side. so we take 0 for this.
The tricky point is to find out the max height from both side, in simplify our logic, we can maintain the two array in advanced, so we can access the inforamtion without extra operation.