对于一个较低的坑来说,其储水量高度取决于左侧最高柱与右侧最高柱的较小高度
可以用两个指针,分别从左右两边往中间逼近,左侧或者右侧的储水量只取决于当前一侧的高度差
if len(height) ==0:
return 0
left,right = 0,len(height)-1
left_max, right_max = height[left], height[right]
r= 0
while left < right:
if height[left] <= height[right]:
if height[left] > left_max:
left_max = height[left]
else:
r += left_max - height[left]
left += 1
else:
if height[right] > right_max:
right_max = height[right]
else:
r += right_max - height[right]
right -= 1
return r