public int largestRectangleArea(int[] heights) {
        if (heights == null) return 0;
        Stack<Integer> s = new Stack<Integer>();
        int maxArea = 0;
        for (int i = 0; i <= heights.length; ++i) {
            int currHeight = i == heights.length ? 0 : heights[i];
            while (!s.isEmpty() && heights[s.peek()] >= currHeight) {
                int prev = s.pop();
                int width = s.isEmpty() ? i : i - 1 - s.peek();
                maxArea = Math.max(maxArea, heights[prev] * width);
            }
            s.push(i);
        }
        return maxArea;
    }