接雨水问题
class Solution {
public:
/**
* max water
* @param arr int整型vector the array
* @return long长整型
*/
long long maxWater(vector<int>& arr) {
// write code here
if (arr.size() == 0)
return 0;
long res = 0;
int left = 0;
int right = arr.size() - 1;
int maxL = 0;
int maxR = 0;
while (left < right) {
maxL = max(maxL, arr[left]);
maxR = max(maxR, arr[right]);
if (maxR > maxL)
res += maxL - arr[left++];
else
res += maxR - arr[right--];
}
return res;
}
};