当前位置:网站首页>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/
边栏推荐
- [effective Objective-C] - memory management
- Postman manage test cases
- Postman pre script - global variables and environment variables
- 02. 开发博客项目之数据存储
- Fluent implements a loadingbutton with loading animation
- Figure database ongdb release v-1.0.3
- Compilation et connexion de shader dans games202 - webgl (comprendre la direction)
- Modbus协议通信异常
- Leetcode dynamic planning day 16
- Pagoda configuration mongodb
猜你喜欢
04. 项目博客之日志
What are the advantages of the industry private network over the public network? What specific requirements can be met?
[lgr-109] Luogu may race II & windy round 6
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
Codeforces Round #804 (Div. 2) Editorial(A-B)
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
Check the useful photo lossless magnification software on Apple computer
浅谈镜头滤镜的类型及作用
02. 开发博客项目之数据存储
[leetcode16] the sum of the nearest three numbers (double pointer)
随机推荐
Drive development - the first helloddk
Excel转换为Lua的配置文件
CUDA11.1在线安装
02. Develop data storage of blog project
Knowledge points of circular structure
RT thread analysis log system RT_ Kprintf analysis
Ora-01779: the column corresponding to the non key value saving table cannot be modified
C Advanced - data storage (Part 1)
nacos-高可用seata之TC搭建(02)
Sorting out the knowledge points of multicast and broadcasting
關於Unity Inspector上的一些常用技巧,一般用於編輯器擴展或者其他
Nacos TC setup of highly available Seata (02)
[noip2009 popularization group] score line delimitation
JDBC calls the stored procedure with call and reports an error
Summary of redis basic knowledge points
指針經典筆試題
Vulhub vulnerability recurrence 71_ Unomi
2021robocom robot developer competition (Preliminary)
Fiddler installed the certificate, or prompted that the certificate is invalid
JS quick start (II)