先从左到右,每次小于左边高度的,加入到暂存temp中,否则记录新位置left,temp存入总量,
再从右到左直到left位置,每次小于右边高度的,加入到暂存temp中,否则记录新位置right,temp存入总量

import java.util.*;


public class Solution {
    /**
     * max water
     * @param arr int整型一维数组 the array
     * @return long长整型
     */
    public long maxWater (int[] arr) {
        // write code here
        int left=0;
        int right=arr.length-1;
        long total=0;

        int i=0;
        long temp=0;
        while(i<=right){
            if(arr[i]<arr[left]){
                temp+=arr[left]-arr[i];
            }else{
                left=i;
                total+=temp;
                temp=0;
            }
            i++;

        }
        i=right;
        temp=0;

        while(i>=left){
            if(arr[i]<arr[right]){
                temp+=arr[right]-arr[i];
            }else{
                right=i;
                total+=temp;
                temp=0;
            }
            i--;
        }
        return total;

    }
}