Formatted question description:

84	Largest Rectangle in Histogram

Given n non-negative integers representing the histogram's bar height where the width of each bar is 1,
find the area of largest rectangle in the histogram.

Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].

The largest rectangle is shown in the shaded area, which has area = 10 unit.

For example,
Given heights = [2,1,5,6,2,3],
return 10.



keyword: “Eliminate the shadow”, that is, the lowest bar is the length of a side

Intuitively, the area of ​​the two bars depends on the shortest and lowest bar among them.

My own doubt is if it is [1,2,3,4,1,2,3,4].

How to prove that the first 1 and last 4 restricted rectangles will definitely not be the largest? Because the stack only maintains the current one, note that there is only one, an increasing sequence.

Simplified to [1,2,1,2], then the maximum area is the area 4 formed by the first 1 and the last 2

  • So the key is that the push and pop conditions of the stack must ensure that the lowest bar is always in the stack.

Extend to eg. [3,1,2,1,2], the largest area also comes from the area formed by the first and last bar at 5.

Because the lowest one will always be in the stack, and the stack will eventually pop to empty, The last pop is the smallest bar. At this time, the shortest bar is multiplied by the length from the first to the last.

The whole question will be clear if you understand this: int currentWidth = sk.isEmpty()? i: (i-1-sk.peek());

sk.peek() is definitely the essence of the quintessence, to find the width of the largest rectangle, I wrote i-currentIndex at the beginning, but I ignored the situation when counting 4 in 2, 8, 8, 4, 6, 8. There are two 88 in front of 4, that is, the width should be from the first 8 to the last 8. At this time, to find this width, you need sk.peek() to find the previous small index

There is another pit, which is the application of dummy end, which has the same effect as dummy head, allowing the for loop to start and run normally.

Arrays.copyOfRange(origin, start, end), if end exceeds the length of the origin array, 0 will be added automatically

The best test case:

  • 2,1,2
  • 2,0,2
  • 2,0,2,1,2



import java.util.Arrays;
import java.util.Stack;

public class Largest_Rectangle_in_Histogram {

    public int largestRectangleArea(int[] rowarray) {

        // stack is to push array index of each element
        Stack<Integer> sk = new Stack<Integer>();

        // length+1, in the end add a dummy element as 0
        int[] row = new int[rowarray.length + 1];
        // @note: Arrays.copyOf()
        row = Arrays.copyOf(rowarray, rowarray.length + 1);

        int max = 0;

        // for (int i = 0; i < row.length; i++) {
        int i = 0;
        while (i < row.length) {

            // @note: careful about duplicate numbers being pushed
            //          "<=" and "<" both AC, since same height bar will be popped and checked anyway, just a matter of when popped
            // if (sk.isEmpty() || row[sk.peek()] <= row[i]) { @note: this is AC also
            if (sk.isEmpty() || row[sk.peek()] < row[i]) {
            } else { // value of index-i is smaller than current stack top

                // @note:  to get lowest bar, since other bars are all larger than it
                int currentLowestBarIndex = sk.pop();
                int edge1 = row[currentLowestBarIndex];
                // @note: empty meaning just popped lowest bar
                // @note: sk.peek() is
                int edge2 = sk.isEmpty() ? i : (i - 1 - sk.peek()); // i-1-peek is ok, since it's monotonic increasing stack

                // calculate area
                max = Math.max(max, edge1 * edge2);

        return max;


All Problems

All Solutions