classSolution: deftrap(self, height: List[int]) -> int: n = len(height) ans = 0 if n <= 1: return ans
left = 0 right = n-1 left_max = 0 right_max = 0 while left < right: left_max = max(left_max, height[left]) right_max = max(right_max, height[right]) if left_max < right_max: ans += left_max - height[left] left += 1 else: ans += right_max - height[right] right = right - 1 return ans