刷题:接雨水
Source : https://leetcode-cn.com/problems/trapping-rain-water/
算法思想: 每一个位置i所能接到的雨水取决于其两边的min(最大值)
class Solution {
public int trap(int[] height) {
if(height == null || height.length < 3) return 0;
int n = height.length;
int res = 0;
int l=0,lm = 0,r=n-1, rm = 0;
while(l<r) {
lm = Math.max(lm, height[l]);
rm = Math.max(rm, height[r]);
if(lm < rm)
res += lm - height[l++];
else
res += rm - height[r--];
}
return res;
}
}
