当前位置:网站首页>Sword finger offer II 039 Maximum rectangular area of histogram
Sword finger offer II 039 Maximum rectangular area of histogram
2022-07-06 05:19:00 【Small white yards fly up】
Summary
Monotonic stack , It's hard to think .
subject
Given an array of nonnegative integers heights , The numbers in the array are used to represent the height of each column in the histogram . Each column is adjacent to each other , And the width is 1 .
In this histogram , The maximum area of a rectangle that can be outlined .

link :https://leetcode.cn/problems/0ynMMM
Ideas
Or use monotone stack . You'll find that , The core of these questions , All use a monotonous stack , Then when traversing the array , Compare the current element with the top element , Always ensure that the top element of the stack is the largest ( Or the smallest ).
The core points of solving this problem are as follows :
- Centered on the height of a column , Spread to both sides to find the boundary . Find a pillar shorter than this one , It's the boundary . At this time, the area can be calculated according to the height of the column and the boundary length .
- Use a monotonic stack whose top is larger than the bottom , To maintain the left boundary .
- If the current element traversed is larger than the top element , The stack .
- If the current element traversed is smaller than the top element , Then the top element of the stack is column , And the position of the current element , Is the rightmost boundary . And because it's a monotonous stack , So the elements below the top of the stack , When the top of the stack is a column , The rightmost boundary .
- Push stack out of stack , Compare the size relationship between the new stack top and the current element , Repeat the logic of the first two
- Traversal complete , Then put the elements that still exist in the stack , Do further calculations . Only when the rightmost column is higher than the front column , That's what happens . So the right boundary at this time , Is the right boundary of the array .
solution : Monotonic stack
Code
public int largestRectangleArea(int[] heights) {
Deque<Integer> stack = new ArrayDeque<>();
// It is convenient to deal with boundary problems
stack.push(-1);
int result = 0;
for (int i = 0; i < heights.length; i++) {
int current = heights[i];
while (stack.peek() != -1 && heights[stack.peek()] > current) {
// Get the subscript of the top of the stack element and remove the top of the stack element
int height = heights[stack.pop()];
int with = i - stack.peek() - 1;
result = Math.max(result, height * with);
}
stack.push(i);
}
// Handle the remaining boundaries in the stack . At this time, the right boundary must be the rightmost end of the array
while (stack.peek()!=-1) {
result = Math.max(result, heights[stack.pop()] * (heights.length - stack.peek() - 1));
}
return result;
}
Submit results

P.S. The animation of this solution is great :https://leetcode.cn/problems/0ynMMM/solution/hua-luo-yue-que-wo-zhen-de-zhen-de-nu-li-ohjt/
边栏推荐
- 2022半年总结
- Codeforces Round #804 (Div. 2)
- Set detailed map + interview questions
- 04. 项目博客之日志
- 01. 开发博客项目之项目介绍
- Driver development - hellowdm driver
- [noip2009 popularization group] score line delimitation
- Summary of redis basic knowledge points
- Postman manage test cases
- In 2022, we must enter the big factory as soon as possible
猜你喜欢
![[classic example] binary tree recursive structure classic topic collection @ binary tree](/img/39/0319c4be43716f927b9d98d89f7655.jpg)
[classic example] binary tree recursive structure classic topic collection @ binary tree

Modbus协议通信异常

Lepton 无损压缩原理及性能分析

Check the useful photo lossless magnification software on Apple computer

04. Project blog log

Notes, continuation, escape and other symbols

Fiddler installed the certificate, or prompted that the certificate is invalid

Pix2pix: image to image conversion using conditional countermeasure networks

TCP three handshakes you need to know

nacos-高可用seata之TC搭建(02)
随机推荐
Check the useful photo lossless magnification software on Apple computer
Compilation et connexion de shader dans games202 - webgl (comprendre la direction)
Oracle deletes duplicate data, leaving only one
[lgr-109] Luogu may race II & windy round 6
Three. JS learning - light and shadow (understanding)
Collection + interview questions
剑指 Offer II 039. 直方图最大矩形面积
从0到1建设智能灰度数据体系:以vivo游戏中心为例
Configuration file converted from Excel to Lua
SQLite add index
Sliding window problem review
Tetris
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Acwing week 58
Force buckle 1189 Maximum number of "balloons"
毕业设计游戏商城
Raspberry pie 3.5-inch white screen display connection
C AES encrypts strings
Figure database ongdb release v-1.0.3
Rce code and Command Execution Vulnerability