public int largestRectangleArea(int[] height) {
        Stack<Integer> stack = new Stack<Integer>();
        int top ;
        int length = height.length;
        int max = 0;
        int i=0;
        while(i < length){
            if(stack.empty() || height[stack.peek()] <= height[i])
                stack.push(i++);
            else{
                top = stack.pop();
                max = Math.max(max, height[top]*(stack.empty() ? i : i-stack.peek()-1));
            }
        }
        while(!stack.empty()){
            top = stack.pop();
            max = Math.max(max, height[top]*(stack.empty() ? i : i-stack.peek()-1));
        }
        return max;

    }