当前位置:网站首页>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/
边栏推荐
- February 12 relativelayout
- Force buckle 1189 Maximum number of "balloons"
- 2022 half year summary
- UCF (2022 summer team competition I)
- flutter 实现一个有加载动画的按钮(loadingButton)
- Biscuits (examination version)
- 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
- jdbc使用call调用存储过程报错
- In 2022, we must enter the big factory as soon as possible
- 图数据库ONgDB Release v-1.0.3
猜你喜欢
04. Project blog log
GAMES202-WebGL中shader的编译和连接(了解向)
[untitled]
Fiddler installed the certificate, or prompted that the certificate is invalid
Questions d'examen écrit classiques du pointeur
从0到1建设智能灰度数据体系:以vivo游戏中心为例
Extension of graph theory
02. 开发博客项目之数据存储
Vulhub vulnerability recurrence 69_ Tiki Wiki
RT thread analysis log system RT_ Kprintf analysis
随机推荐
jdbc使用call调用存储过程报错
【torch】|torch. nn. utils. clip_ grad_ norm_
Unity gets the width and height of Sprite
指針經典筆試題
Upload nestjs configuration files, configure the use of middleware and pipelines
Pickle and savez_ Compressed compressed volume comparison
Questions d'examen écrit classiques du pointeur
SQLite queries the maximum value and returns the whole row of data
Postman test report
Golang -- TCP implements concurrency (server and client)
Excel转换为Lua的配置文件
Pix2pix: image to image conversion using conditional countermeasure networks
Pointer classic written test questions
UCF (summer team competition II)
集合详解之 Map + 面试题
MySQL advanced learning summary 9: create index, delete index, descending index, and hide index
MySQL time processing
驱动开发——第一个HelloDDK
C进阶-数据的存储(上)
Acwing week 58