Solution
-
The ‘height’ of the water that one col can trap is NOT decied by the col height. It is decided by the maximum height of neighbours to the left and right.
-
Use two array to store the maximum height of neighbours for each col from left and right.
-
The lower one of
start[i]
andend[i]
decides the height of water, and theheight[i]
decides the occupied part.
def trap(self, height):
"""
:type height: List[int]
:rtype: int
"""
if len(height) == 0:
return 0
start, end = [height[0]], [height[-1]]
res = 0
for h in height[1:]:
start.append(max(start[-1], h))
for h in reversed(height[1:]):
end.append(max(end[-1], h))
end = list(reversed(end))
for i, h in enumerate(height):
res += max(0, min(start[i], end[i]) - height[i])
return res
PREVIOUSEdit Distance