Skip to content

Latest commit

 

History

History
19 lines (16 loc) · 532 Bytes

File metadata and controls

19 lines (16 loc) · 532 Bytes
class Solution:
    def trap(self, height: List[int]) -> int:
        left, right = 0, len(height) - 1
        ans, left_max, right_max = 0, 0, 0
        while (left <= right):
            if (left_max < right_max):
                left_max = max(left_max, height[left])
                ans += left_max - height[left]
                left += 1
            else:
                right_max = max(right_max, height[right])
                ans += right_max - height[right]
                right -= 1
        return ans