单调递减栈 出现大的数 取出栈顶元素开始计算面积

import java.util.Stack;



public class Solution {
    /**
     * max water
     * @param arr int整型一维数组 the array
     * @return long长整型
     */
    public long maxWater (int[] arr) {
        // write code here
        int len = arr.length;
        if(len == 0 || arr == null){
            return 0;
        }
        Stack<Integer> stack = new Stack();
        long ans = 0;
        for(int i = 0;i<len;i++){
            while(!stack.isEmpty()&&arr[i]>arr[stack.peek()]){
                int cur = stack.pop();
                if(stack.isEmpty()){
                    break;
                }
                int left = stack.peek();
                long width = i-left-1;
                long height = Math.min(arr[i],arr[left])-arr[cur];
                ans+=width*height;
            }
            stack.push(i);
        }
        return ans;
    }
}