LeetCode Brushing Notes _84. The largest rectangle in histogram

The topic is from LeetCode

84. The largest rectangle in the histogram

Other solutions or source code can be accessed: tongji4m3

description

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.

Example:

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

Ideas

The overall idea is that the largest rectangle is to traverse the array once, look at the rectangles formed by index i as the height, and take their maximum value

Maintain an increasing sequence. When encountering a decreasing element, the rectangle that can be formed by the previous element higher than it has been determined and can be calculated. Then continue until the array is traversed, at which point there is an increasing sequence.

For an element nums[i] in this sequence, its right boundary is the last element of the array, and the left boundary is the next stack element +1 (draw it for consideration)

liweiwei1419's solution

Stack<Integer> stack;//递增序列 存储的是索引下标 
for i in N
{
	//nums[stack.top()]=nums[i]的要留下,因为前面的元素还可以继续扩展
    while(!stack.isEmpty() && nums[stack.top()]>nums[i])
	{
		//这里计算的是以弹出那个元素为高度的最大矩形
		int index=stack.pop();
		result=max(result,(i-index)*nums[index]);
	}
	stack.push(i);//此时为递增序列
}
//对递增序列进行计算
//这些元素每一个都可以扩展到数组尽头
while(!stack.isEmpty())
{
	//范围是[ stack.top()+1 , N-1 ]
	int index=stack.pop();
	result=max(result,(N-stack.top()-1)*nums[index]);
}

detail

  1. The bottom of the stack is marked with a -1, otherwise there is a problem in calculating the left boundary of the element with index 0
  2. Note that the calculation of the left boundary cannot be directly index i (because it should be noted that the elements on the left that are higher than him are ignored and not in the stack)
  3. Note that the calculation is based on the value of the index position as high, and the maximum distance that can be extended on the left and right sides is the width

Code

public int largestRectangleArea(int[] heights)
{
    
    
    //递增序列 存储的是索引下标
    Stack<Integer> stack = new Stack<>();
    int result = 0;
    int n = heights.length;
    //防止最左边元素计算不了面积
    stack.push(-1);
    for (int i = 0; i < n; i++)
    {
    
    
        //heights[stack.top()]=heights[i]的要留下,因为前面的元素还可以继续扩展
        while (stack.peek() != -1 && heights[stack.peek()] > heights[i])
        {
    
    
            //这里计算的是以弹出那个元素为高度的最大矩形
            int index = stack.pop();
            //look 这里也要按照下面的思路,即该元素的左边界是stack.top()+1 而不能仅仅是(i - index) * heights[index]
            result = Math.max(result, (i - stack.peek() - 1) * heights[index]);
        }
        //此时为递增序列
        stack.push(i);
    }
    //对递增序列进行计算
    //这些元素每一个都可以扩展到数组尽头
    while (stack.peek() != -1)
    {
    
    
        //范围是[ stack.top()+1 , N-1 ]
        int index = stack.pop();
        result = Math.max(result, (n - stack.peek() - 1) * heights[index]);
    }
    return result;
}

Complexity analysis

time complexity

O (N) O (N) O ( N )

Space complexity

O (N) O (N) O ( N )

Guess you like

Origin blog.csdn.net/weixin_42249196/article/details/108396723