12345678910111213141516171819202122 |
- from typing import List
- class Solution:
- def trap(self, height: List[int]) -> int:
- res = 0
- left, right = 0, len(height) - 1
- lMax, rMax = 0, 0
- while left < right:
- lMax = max(lMax, height[left])
- rMax = max(rMax, height[right])
- if lMax < rMax:
- res += lMax - height[left]
- left += 1
- else:
- res += rMax - height[right]
- right -= 1
- return res
- if __name__ == '__main__':
- print(Solution().trap([0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]))
|