思路
from typing import List
import bisect
class Solution:
def trap(self, height: List[int]) -> int:
def helper(height, reverse=False):
index, ans, left_max_h_index, n, water = 0, 0, 0, len(height), 0
if reverse: height.reverse()
height, n = height + [0], n + 1
for index, h in enumerate(height):
if index == 0 or index == n - 1: continue
# 可以做右边界
if h >= height[index + 1] and h >= height[index - 1] and height[left_max_h_index] <= h:
# 左边最高的比他低,那么一定会形成一个区间。
ans += water
left_max_h_index = index
water = 0
else:
water += max(0, (height[left_max_h_index] - h))
return ans, left_max_h_index
x, left_max_h_index = helper(height)
y, _ = helper(height[left_max_h_index:], reverse=True)
return x+y
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。