题解 | #接雨水问题#
接雨水问题
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) {
// write code here
long long res = 0;
int left=0, right = arr.size()-1;
int left_h = arr[left], right_h = arr[right];
while(left < right){
if(left_h <= right_h){
if(left_h < arr[left]){
left_h = arr[left];
}
else{
res += left_h - arr[left];
left++;
}
}
else{
if(right_h < arr[right]){
right_h = arr[right];
}
else{
res += right_h - arr[right];
right--;
}
}
}
return res;
}
};