题解 | #接雨水问题#
接雨水问题
https://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
int left=0,right=arr.size()-1,res=0;
int lmax=0,rmax=0;
while(left<right){
lmax=max(lmax,arr[left]);
rmax=max(rmax,arr[right]);
if(lmax<rmax){
res+=lmax-arr[left];
left++;
}
else {
res+=rmax-arr[right];
right--;
}
}
return res;
}
};


联想公司福利 1481人发布