Buckle 84. Largest rectangle in histogram

This is a very typical problem that uses a monotonic stack to solve the problem of order and size synthesis. The monotonic stack is particularly suitable for solving problems where the size of the two ends determines the size of the middle value.

Because in the monotonous stack, the monotonous increase stack is used as an example. Let the element at the top of the stack be b, and the second element at the top of the stack be a. Naturally, a <b (because the stack grows larger). At this time,
if c appears and c is less than b, then the first left and right of b is greater than b The two small elements are found, namely a and c, and b is the largest in the middle.
At this time you can process b and reorganize the stack to keep it increasing. If c is greater than b, then c is pushed onto the stack and the loop continues. Finally clean up the stack

/ ********
Author: allen-238
link: https: //leetcode-cn.com/problems/largest-rectangle-in-histogram/solution/ji-jian-python-shen-du-jie- xi-dan-diao-dui-zhan-zu /
Source: LeetCode The copyright belongs to the author. For commercial reproduction, please contact the author for authorization, and for non-commercial reproduction, please indicate the source.
********* /

for i  in  list:
    while i is not empty and stack[-1] > i: 先调整位置。
        stack.pop()
    stack.append(i)  #当前元素无论如何也要放进去,不同的只是需不需要pop来调整位置

作者:allen-238
链接:https://leetcode-cn.com/problems/largest-rectangle-in-histogram/solution/ji-jian-python-shen-du-jie-xi-dan-diao-dui-zhan-zu/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

The detail to note here is that the
bottom of the stack should be filled with -1, indicating the bottom of the stack.
At the end of the cycle, the stack must be cleaned up. At this time, the right boundary c of all elements that continue to be stored in the stack is the end len (height) -1

class Solution:
    def largestRectangleArea(self, heights: List[int]) -> int:
        stack = [-1]
        max_res = 0
        for i in range(len(heights)):
            while len(stack) > 1 and heights[i] <= heights[stack[-1]]:
                max_res = max(max_res, heights[stack.pop()] * (i - stack[-1] - 1))  
            stack.append(i)
        for i in range(len(stack)-1):
            max_res = max(max_res, heights[stack.pop()]*(len(heights)-1-stack[-1]))
        return max_res

作者:allen-238
链接:https://leetcode-cn.com/problems/largest-rectangle-in-histogram/solution/ji-jian-python-shen-du-jie-xi-dan-diao-dui-zhan-zu/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

My own solution: actually refer to others. In fact, the brute force method solves the maximum value of the rectangular area with each rectangular bar as the minimum height . C ++ code:

#include <iostream>

using namespace std;

const int N = 10000;
int h[N];

int main()
{
    int n, s_max = 0, i = 0,ileft,iright,ih;
    cin >> n;

    while(i < n){
        cin >> h[i];
        i++;
    }
    for(i = 0;i < n;i++){
        int left = i - 1, right = i + 1;
        while(left >= 0 && h[left] >= h[i])--left;
        while(right < n && h[right] >= h[i]) ++right;
        int tmp = (right - left - 1) * h[i];
        s_max = s_max > tmp ? s_max:tmp;
    }
    cout<<s_max<<endl;
    return 0;
}
Published 21 original articles · praised 0 · visits 81

Guess you like

Origin blog.csdn.net/weixin_43963453/article/details/105396262