参考了一下,没想出来,呵呵

class Solution {
public:
    /**
     * max water
     * @param arr int整型vector the array
     * @return long长整型
     */
    long long maxWater(vector<int>& arr) {
        if(arr.size() <= 2){
            return 0;
        }

//         int max = arr[0];
//         int maxIndex = 0;
//         for(int i=1;i<arr.size();i++){
//             if(arr[i] > max){
//                 max = arr[i];
//                 maxIndex = i;
//             }
//         }
        
//         long long water = 0;
//         int side = arr[0];
//         for(int i =1;i<maxIndex;i++){
//             if(arr[i] > side){
//                 side = arr[i];
//             }else{
//                 water += (side-arr[i]);
//             }
//         }
        
//         side = arr[arr.size()-1];
//         for(int i = arr.size()-2;i>maxIndex;i--){
//             if(arr[i] > side){
//                 side = arr[i];
//             }else{
//                 water += (side-arr[i]);
//             }
//         }
        long long water = 0;
        int left = 0;
        int right = arr.size()-1;

        int leftVal = arr[left];
        int rightVal = arr[right];
        while(left < right){
            if(leftVal <= rightVal){
                if(arr[left+1] > leftVal){
                    leftVal = arr[left+1];
                }else{
                    water += (leftVal-arr[left+1]);
                }
                left++;
            }else{
                if(arr[right-1] > rightVal){
                    rightVal = arr[right-1];
                }else{
                    water += (rightVal-arr[right-1]);
                }
                right--;
            }
        }
        return water;
    }
};