public int largestRectangleArea(int[] height) {

        Stack<Integer> stack = new Stack<Integer>();
        int i = 0;
        int maxArea = 0;
        //push in order, increase
        //if smaller than previous one, pop and calculate
        while (i < height.length) {
            if (stack.isEmpty() || height[stack.peek()] <= height[i]) {
                stack.push(i++);
            } else {
                int t = stack.pop();
                maxArea = Math.max(maxArea, height[t] * (stack.isEmpty() ? i : i - stack.peek() - 1));
            }
        }

        //calculate the rest height
        while (stack.empty() == false) {
            int t = stack.pop();
            maxArea = Math.max(maxArea, height[t] * (stack.isEmpty() ? i : i - stack.peek() - 1));
        }

        return maxArea;
    }