• + 0 comments

    Java 8

    ` public static int largestRectangle(List h) { int start = 0; int index = 0; int maxArea = 0; Stack stack = new Stack<>();

        for (int i = 0; i < h.size(); i++) {
                int height = h.get(i);
                start = i;
                while (!stack.isEmpty() && stack.peek()[1] > height) {
                        int[] top = stack.pop();
                        index = top[0];
                        int poppedHeight = top[1];
                        maxArea = Math.max(maxArea, poppedHeight * (i - index));
                        start = index;
                }
                stack.push(new int[]{start, height});
        }
    
        for (int[] pair : stack) {
                int height = pair[1];
                maxArea = Math.max(maxArea, height * (h.size() - pair[0]));
        }
    
        return maxArea;
    

    } `