[One question of the day] Likou 84: The largest rectangle in the histogram

Title description ( portal )

Given n non-negative integers, they are used to represent the height of each column in the histogram. Each pillar is adjacent to each other and has a width of 1.
Find the maximum area of ​​the rectangle that can be outlined in the histogram.
Insert picture description here

The above is an example of a histogram, where the width of each column is 1, and the given height is [2,1,5,6,2,3].
Insert picture description here

The shaded part in the figure is the largest rectangular area that can be outlined, and its area is 10 units.

Example

示例:

输入: [2,1,5,6,2,3]
输出: 10

Problem solving ideas

The brute force solution
is to first count the largest value high in the heights array, and then traverse brutally from 1 to high with high.

public static int largestRectangleArea(int[] heights) {
    
    
        //    [2,1,5,6,2,3]
        if (heights.length < 1) {
    
    
            return 0;
        }
        int len = heights.length;
        int high = heights[0];
        for (int i = 0; i < len ; i++) {
    
    
            if (heights[i] > high) {
    
    
                high = heights[i]; //最高的高度
                //System.out.println(high);
            }
        }
        int maxarea = 0;
        for (int i = 1; i <= high; i++) {
    
    
            int maxlen = 0;
            int tmp = 0;
            for (int j = 0; j < len; j++) {
    
    
                if (heights[j] >= i) {
    
    
                    tmp++;
                }else {
    
    
                    maxlen = Math.max(maxlen, tmp);// 更新最大长度
                    tmp = 0;
                }
            }
            maxlen = Math.max(maxlen,tmp);//一定注意这里,因为上边循环退出,可能是最后一个高度高于i,也有可能不是。当是最后一个最高时刚好结束了循环,那么就没有去更新maxlen。
            maxarea = Math.max(maxarea, maxlen * i);// 更新最大面积
        }
        return maxarea;

    }

Here I have traversed every height, the time complexity is too high. This can be optimized, instead of traversing every height, just like the problem of wooden barrels of water, the maximum value of water depends on the shortest board. So only need to traverse the height value it has (the height of the largest area must be a value in the existing elements of the array).

public static int largestRectangleArea(int[] heights) {
    
    
        //    [2,1,5,6,2,3]
        if (heights.length < 1) {
    
    
            return 0;
        }
        int len = heights.length;
        int high = heights[0];

        int maxarea = 0;
        for (int i = 0; i < len; i++) {
    
    
            if (heights[i] > high) {
    
    
                high = heights[i]; //最高的高度
                //System.out.println(high);
            }
            int high_tmp = heights[i];

            int maxlen = 0;
            int tmp = 0;
            for (int j = 0; j < len; j++) {
    
    
                if (heights[j] >= high_tmp) {
    
    
                    tmp++;
                }else {
    
    
                    maxlen = Math.max(maxlen, tmp);// 更新最大长度
                    tmp = 0;
                }
            }
            maxlen = Math.max(maxlen,tmp);//一定注意这里,因为上边循环退出,可能是最后一个高度高于i,也有可能不是。当是最后一个最高时刚好结束了循环,那么就没有去更新maxlen。
            maxarea = Math.max(maxarea, maxlen * high_tmp);// 更新最大面积
        }
        return maxarea;

    }

The solution to the monotonic stack will be supplemented after I study it later.

Guess you like

Origin blog.csdn.net/weixin_45532227/article/details/111768770