题解 | #接雨水问题#
接雨水问题
http://www.nowcoder.com/practice/31c1aed01b394f0b8b7734de0324e00f
双指针往中间移动,若lmax>rmax则hi--,否则lo++
# # max water # @param arr int整型一维数组 the array # @return long长整型 # class Solution: def maxWater(self , arr ): # write code here n = len(arr) if n <= 3: return 0 lo, hi = 0, n-1 lmax = arr[lo] rmax = arr[hi] ans = 0 while lo <= hi: lmax = max(lmax, arr[lo]) rmax = max(rmax, arr[hi]) if lmax > rmax: ans += rmax-arr[hi] hi -= 1 else: ans += lmax-arr[lo] lo += 1 return ans