C ++] LeetCode: 133 Largest Rectangle in Histogram (maximum rectangular area)
Question:
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 =10Unit.
For example,
Given height =[2,1,5,6,2,3],
Return10.
An1_1: Brute Force
Ideas:For every bar in the histogram, we raise all the left boundary to record the largest area. The time complexity is O (N ^ 2). However, the leetcode uses the simple exhaustive method to talk about TLE. We need some pruning methods, such as finding the right boundary. We found that when height [k]> = height [k-1], whatever the value of the left boundary, choosing height [k] As the right boundary is always larger than selecting height [k-1. Therefore, when selecting the right boundary, we first find a k of height [k]
Another idea is to use the final method: Take the current bar as the rectangle height, and then move the two pointers to both sides of the array until the height is lower than the current bar Height, then, multiply the height of the current bar by the width of the two sides, which is the maximum area. Because we have calculated the maximum matrix with the target height for all bar values, the best results will surely be obtained. However, it still times out without pruning.
Attention:
1. In case of exhaustion, we need to maintain a minimum height forward from the current k.
int lowest = height[i]; for(int j = i; j >= 0; j--) { lowest = min(height[j], lowest);2. Pay attention to the pruning method.
for(int k = i + 1; k < height.size(); k++) { if(height[k] < height[k-1]) { i = k - 1; break; } else { i = k; } }
AC Code:
class Solution {public: int largestRectangleArea(vector
&height) { int maxarea = 0; for(int i = 0; i < height.size(); i++) { for(int k = i + 1; k < height.size(); k++) { if(height[k] < height[k-1]) { i = k - 1; break; } else { i = k; } } int lowest = height[i]; for(int j = i; j >= 0; j--) { lowest = min(height[j], lowest); int curarea = lowest * (i - j + 1); maxarea = max(maxarea, curarea); } } return maxarea; }};
Answer 2: Stack Optimization Algorithm
Ideas:We maintain a stack. The stack increases progressively from the lower to the higher. If the bar height is lower than the top element of the stack, the stack goes out until the conditions are met, in this process, we constantly regard the top elements of the stack as the minimum height to calculate the maximum area. The key issue lies in how to determine the maximum range of the height corresponding to the stack element during stack exit. If the stack is empty, all elements so far (except the current subscript element) are higher than the height of the elements in the output stack (otherwise, there will be elements in the stack ), then the matrix area is the top element height of the stack. h [t] * Current subscript I. if the stack is not empty, the height of the top element of the current stack is higher than that of the top element of the current stack to that of the subscript element, because the top element of the current stack is smaller than the top element of the output stack. For details, see the figure below:
Note the area calculation during the stack play process.
H [t] * (stack. isEmpty ()? I: I-stack. peek ()-1)
H [t]Is the top element of the pop-up stack. At this time, the area is calculated as the largest area that can be enclosed by h [t] and the "Upper Class Society" in front. Note that the indexes in the stack point to elements smaller than h [t]. If h [t] is currently the smallest, the stack is empty. At present, the top elements of the stack and h [t] (excluding h [t] and top elements of the stack) are greater than the two.
We need to note that we should clear and judge the remaining stacks at last, because the algorithm judges the area of the previous elements each time. If there are still elements in the stack after the loop ends, continue to maintain the area until the stack is empty.
AC Code:
Class Solution {public: int largestRectangleArea (vector
& Height) {stack
Stk; int maxarea = 0; int tp; // The int area_with_tp element on the top of the stack; // The area of the shortest board with the top element of the stack as int I = 0; while (I
This question has two blog posts to help you understand.
Largest Rectangle in HistogramLargest Rectangular Area in a Histogram | Set 2 is difficult to understand. Think carefully. The optimization method only needs to be scanned once. The time complexity is O (N), the space complexity is the stack size, and the worst is O (N ).
There is also an extended question Maximal Rectangle. Using this question as a subroutine is a complicated question.