当前位置:网站首页>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/
边栏推荐
- Figure database ongdb release v-1.0.3
- Compilation and connection of shader in games202 webgl (learn from)
- Please wait while Jenkins is getting ready to work
- 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
- Application of Flody
- Pagoda configuration mongodb
- 图数据库ONgDB Release v-1.0.3
- Codeforces Round #804 (Div. 2)
- 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
- JS quick start (II)
猜你喜欢
从0到1建设智能灰度数据体系:以vivo游戏中心为例
Nacos - TC Construction of High available seata (02)
js Array 列表 实战使用总结
02. Develop data storage of blog project
Fiddler installed the certificate, or prompted that the certificate is invalid
Hyperledger Fabric2. Some basic concepts of X (1)
nacos-高可用seata之TC搭建(02)
nacos-高可用seata之TC搭建(02)
JS quick start (II)
【OSPF 和 ISIS 在多路访问网络中对掩码的要求】
随机推荐
Three.js学习-光照和阴影(了解向)
Can the feelings of Xi'an version of "Coca Cola" and Bingfeng beverage rush for IPO continue?
[noip2009 popularization group] score line delimitation
Configuration file converted from Excel to Lua
你需要知道的 TCP 三次握手
剑指 Offer II 039. 直方图最大矩形面积
组播和广播的知识点梳理
Leetcode dynamic planning day 16
[classic example] binary tree recursive structure classic topic collection @ binary tree
指針經典筆試題
Questions d'examen écrit classiques du pointeur
Compilation et connexion de shader dans games202 - webgl (comprendre la direction)
Golang -- TCP implements concurrency (server and client)
Acwing week 58
Rce code and Command Execution Vulnerability
In 2022, we must enter the big factory as soon as possible
Set detailed map + interview questions
图数据库ONgDB Release v-1.0.3
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
February 12 relativelayout