当前位置:网站首页>[Li Kou brush question] monotone stack: 84 The largest rectangle in the histogram
[Li Kou brush question] monotone stack: 84 The largest rectangle in the histogram
2022-06-26 16:22:00 【Li xiaoenzi】
subject :
Given n Nonnegative integers , It is used to indicate 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 .

Ideas :
// Monotonically increasing stacks , For the column in the stack , The first column on the left whose height is less than its own is under itself
// Traverse each cylinder , If the current column height is greater than or equal to the height of the stack top column , It into the stack
// Otherwise, you will find the first column smaller than itself on the right side of the stack top element , Top of stack element , At the same time, the maximum area of the rectangle corresponding to the top element of the stack can be calculated
// Add a to the last element of the array 0, Use this condition to get all the elements in the stack out of the stack
// For special treatment on the left , If the stack is empty after an element at the top of the stack is out of the stack , It means that no element on the left is shorter than it , It is the shortest of its kind , The left side can be extended to 0
class Solution {
public int largestRectangleArea(int[] heights) {
// Monotonically increasing stacks , For the column in the stack , The first column on the left whose height is less than its own is under itself
// Traverse each cylinder , If the current column height is greater than or equal to the height of the stack top column , It into the stack
// Otherwise, you will find the first column smaller than itself on the right side of the stack top element , Top of stack element , At the same time, the maximum area of the rectangle corresponding to the top element of the stack can be calculated
// Add a to the last element of the array 0, Use this condition to get all the elements in the stack out of the stack
// For special treatment on the left , If the stack is empty after an element at the top of the stack is out of the stack , It means that no element on the left is shorter than it , It is the shortest of its kind , The left side can be extended to 0
int[] newH = new int[heights.length + 1];
for(int i = 0; i < heights.length;i++){
newH[i] = heights[i];
}
newH[heights.length] = 0;
Stack<Integer> stack = new Stack<>();
int res = 0;
for(int i = 0;i < heights.length + 1;i++){
while(stack.size() != 0 && newH[stack.peek()] > newH[i]){
res = Math.max(res,newH[stack.pop()] * (stack.size() == 0 ? i : (i - stack.peek() - 1)));
}
stack.push(i);
}
return res;
}
}边栏推荐
- 当一个程序员一天被打扰 10 次,后果很惊人!
- 大话领域驱动设计——表示层及其他
- Ten thousand words! In depth analysis of the development trend of multi-party data collaborative application and privacy computing under the data security law
- (1) Keras handwritten numeral recognition and recognition of self written numbers
- Solution for filtering by special string of microservice
- Detailed explanation of cookies and sessions
- Anaconda3 installation tensorflow version 2.0 CPU and GPU installation, win10 system
- IAR工程适配GD32芯片
- 11 introduction to CNN
- 100+数据科学面试问题和答案总结 - 基础知识和数据分析
猜你喜欢

Niuke programming problem -- dynamic programming of must brush 101 (a thorough understanding of dynamic programming)

11 cnn简介

理想路径问题

Angel 3.2.0 new version released! Figure the computing power is strengthened again

Arduino uno + DS1302 simple time acquisition and serial port printing
Scala 基础 (二):变量和数据类型

The first batch in the industry! Tencent cloud security and privacy computing products based on angel powerfl passed CFCA evaluation

大话领域驱动设计——表示层及其他

基于 MATLAB的自然过渡配音处理方案探究

3 keras版本模型训练
随机推荐
请指教同花顺软件究竟是什么?网上开户是否安全么?
清华“神奇药水”登Nature:逆转干细胞分化,比诺奖成果更进一步,网友:不靠精子卵子就能创造生命了?!...
R language plot visualization: plot visualizes the normalized histogram, adds the density curve KDE to the histogram, and uses geom at the bottom edge of the histogram_ Adding edge whisker graph with
Hyperf框架使用阿里云OSS上传失败
JS教程之使用 ElectronJS 桌面应用程序打印贴纸/标签
Redis的ACID
IAR工程适配GD32芯片
Stepn novice introduction and advanced
R语言广义线性模型函数GLM、glm函数构建逻辑回归模型(Logistic regression)、分析模型是否过离散(Overdispersion)、使用残差偏差与二项式模型中的剩余自由度的比率评估
精致妆容成露营“软实力”,唯品会户外美妆护肤产品销量激增
This year, the AI score of college entrance examination English is 134. The research of Fudan Wuda alumni is interesting
Solidus labs welcomes zhaojiali, former head of financial innovation in Hong Kong, as a strategic adviser
7 自定义损失函数
【小5聊】毕业8年,一直在追梦的路上
[from database deletion to running] JDBC conclusion (finish the series in one day!! run as soon as you finish learning!)
Learn about common functional interfaces
若依微服务特殊字符串被过滤的解决办法
R语言使用cor函数计算相关性矩阵进行相关性分析,使用corrgram包可视化相关性矩阵、行和列使用主成分分析重新排序、下三角形中使用平滑的拟合线和置信椭圆,上三角形中使用散点图、对角线最小值和最大值
理想路径问题
100+ data science interview questions and answers Summary - basic knowledge and data analysis