题解 | #接雨水问题#
接雨水问题
http://www.nowcoder.com/practice/31c1aed01b394f0b8b7734de0324e00f
双指针入门题
class Solution {
public:
/**
* max water
* @param arr int整型vector the array
* @return long长整型
*/
long long maxWater(vector<int>& arr) {
long long res = 0;
int l = 0;
int _max = arr[0];
for(int r=1;r<arr.size();r++){
if(arr[r]<=_max)continue;
else {
while(l<r){
res += _max-arr[l];
l++;
}
_max = arr[r];
}
}
_max = arr[arr.size()-1];
int r = arr.size()-1;
for(int i=arr.size()-1;i>=l;i--){
if(arr[i]<_max)continue;
else {
while(i<r){
res += _max - arr[r];
r--;
}
_max = arr[i];
}
}
return res;
}
};