[84] leetcode histogram largest rectangle (dynamic programming, the stack)

Topic links: https://leetcode-cn.com/problems/largest-rectangle-in-histogram/

Title Description

Given n non-negative integer that represents the height of each column in the histogram. Each column adjacent to each other, and a width of 1.

In seeking this histogram, the maximum area can be outlined rectangle.

Here Insert Picture Description

The above is an example of the histogram, wherein the width of each column is 1, given that the height of [2,1,5,6,2,3].

Here Insert Picture Description

The shaded portion is the maximum that can be outlined by a rectangular area, an area of ​​10 units.

Thinking

1 violence

The iposition as the left boundary, jtraversing i~nobtain (i,j)the minimum height section, (j-i+1)*minHeightobtained in ia left end point, jthe maximum area of the rectangle as the right point.

Complexity Analysis

time complexity: O ( n 2 ) O (n ^ 2) . We need to enumerate all possible columns right.
Space complexity: O ( 1 ) O (1) . No additional space.

class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        if (heights.empty()) return 0;
        int maxArea = 0;
        for (int i = 0; i < heights.size(); ++i) {
            int minHeight = heights[i];
            for (int j = i; j < heights.size() && heights[j]!=0; ++j){
                minHeight = min(minHeight, heights[j]);
                maxArea = max(maxArea,minHeight*(j-i+1));
            }
        }
        return maxArea;
    }
};

Unfortunately, overtime =. =!
Here Insert Picture Description

Stack 2

In this method, we maintain a stack. At first, we put -1into the top of the stack to indicate the start. During initialization, in order from left to right, we will continue to post the serial number into the stack until it encounters the adjacent columns declining relationship, that is a[i-1] > a[i]a[i−1]>a[i]. Now, we begin to stack pop number, until it encounters stack[j]meet a[stack[j]]≤a[i]. Every time we pop subscript, we use as the largest rectangular area pop-up element formed of a high current element width is stack[top-1]that between the columns. That is, when we pop stack[top], the record of the current element in the original array index for ithe current pop-up element is a high maximum rectangular area:

(i−stack[top−1]−1)×a[stack[top]].

Furthermore, when we arrived at the end of the array, we will stack the remaining elements all popped off the stack. In the pop-up each element, we use the following formula to find the area: (stack[top]−stack[top−1])×a[stack[top]]which stack[top]represents just been pop elements. Therefore, we can get the most out of each rectangle rectangular area by area is relatively new calculation.

Complexity analysis
time complexity: O ( n ) O (n) . are each n digital push popping each time.
Space complexity: O ( n ) O (n) . Used to store the stack of elements.

/*
 * 单调栈
 * 时间复杂度O(n) 空间复杂度O(n)
 */
class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        if (heights.empty()) return 0;
        stack<int> s;   // 存储序号
        s.push(-1);
        int maxArea = 0;
        for (int i = 0; i < heights.size(); ++i) {
            // 下降时pop
            while (s.top() != -1 && heights[s.top()] >= heights[i]){
                int tmp = s.top();
                s.pop();
                // 用弹出元素作为高形成的最大面积矩形,其宽是当前元素与s[top-1]之间的那些柱子
                maxArea = max(maxArea, heights[tmp] * (i-s.top() - 1));
            }
            s.push(i);
        }
        while (s.top()!= -1){
            int tmp = s.top();
            s.pop();
            int curArea = heights[tmp] * (heights.size() - s.top() - 1);
            maxArea = max(maxArea, curArea);
        }

        return maxArea;
    }
};

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/zjwreal/article/details/91477800