Article directory
One [topic category]
- the stack
Two [question difficulty]
- difficulty
Three [topic number]
- 84. Largest rectangle in histogram
Four [title description]
- Given n non-negative integers, used to represent the height of each column in the histogram. Each column 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.
Five [topic examples]
-
Example 1:
- Input: heights = [2,1,5,6,2,3]
- Output: 10
- Explanation: The largest rectangle is the red area in the figure, with an area of 10
-
Example 2:
- Input: heights = [2,4]
- Output: 4
Six [topic prompt]
- 1 < = h e i g h t s . l e n g t h < = 1 0 5 1 <= heights.length <=10^5 1<=heights.length<=105
- 0 < = h e i g h t s [ i ] < = 1 0 4 0 <= heights[i] <= 10^4 0<=heights[i]<=104
Seven [problem-solving ideas]
- Using the idea of monotonic stack, this monotonic stack is an increasing monotonic stack
- For the largest rectangle that a certain column can form, our purpose is to find the first column on the left and right that is smaller than the height of this column (short board effect)
- So we traverse the array from left to right. If the height of the current column is greater than or equal to the height of the top column of the stack, then it will be pushed onto the stack. Because we maintain a monotonically increasing stack, for the current column, its left boundary It is the current top element of the stack; if the height of the current column is smaller than the height of the top column, it means that the right boundary has been found, and the current column is the right boundary of the top column
- When we find the left and right boundaries, the difference is the bottom of the rectangle, and then multiplied by the height of the current column, it is the largest rectangle that the current column can form
- Traversing the array comparison to find the maximum rectangle value
- Finally return the result
Eight 【Time Frequency】
- Time complexity: O ( n ) O(n)O(n), n n n is the length of the incoming array
- Space Complexity: O ( n ) O(n)O(n), n n n is the length of the incoming array
Nine [code implementation]
- Java language version
class Solution {
public int largestRectangleArea(int[] heights) {
int[] temp = new int[heights.length + 2];
System.arraycopy(heights,0,temp,1,heights.length);
Deque<Integer> stack = new LinkedList<Integer>();
int res = 0;
for(int i = 0;i<temp.length;i++){
while(!stack.isEmpty() && temp[i] < temp[stack.peek()]){
int h = temp[stack.pop()];
int right = i;
int left = stack.peek();
int w = right - left - 1;
res = Math.max(res,w * h);
}
stack.push(i);
}
return res;
}
}
- C language version
int largestRectangleArea(int* heights, int heightsSize)
{
int* temp = (int*)calloc(heightsSize + 2,sizeof(int));
for(int i = 1;i <= heightsSize;i++)
{
temp[i] = heights[i - 1];
}
int* stack = (int*)calloc(heightsSize + 2,sizeof(int));
int top = -1;
int res = 0;
for(int i = 0;i < heightsSize + 2;i++)
{
while(top != -1 && temp[i] < temp[stack[top]])
{
int h = temp[stack[top--]];
int right = i;
int left = stack[top];
int w = right - left - 1;
res = fmax(res,w * h);
}
stack[++top] = i;
}
return res;
}
- Python language version
class Solution:
def largestRectangleArea(self, heights: List[int]) -> int:
heights.append(0)
heights.insert(0,0)
stack = []
res = 0
for i in range(0,len(heights)):
while len(stack) != 0 and heights[i] < heights[stack[-1]]:
h = heights[stack.pop()]
right = i
left = stack[-1]
w = right - left - 1
res = max(res,w * h)
stack.append(i)
return res
- C++ language version
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
heights.insert(heights.begin(),0);
heights.push_back(0);
stack<int> st;
int res = 0;
for(int i = 0;i<heights.size();i++){
while(!st.empty() && heights[i] < heights[st.top()]){
int h = heights[st.top()];
st.pop();
int right = i;
int left = st.top();
int w = right - left - 1;
res = fmax(res,w * h);
}
st.push(i);
}
return res;
}
};
Ten【Submission Results】
-
Java language version
-
C language version
-
Python language version
-
C++ language version