当前位置:网站首页>LeetCode·84.柱状图中最大的矩形·单调递增栈
LeetCode·84.柱状图中最大的矩形·单调递增栈
2022-08-04 15:51:00 【小迅想变强】
链接:https://leetcode.cn/problems/largest-rectangle-in-histogram/solution/by-xun-ge-v-626m/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
题目

示例
思路
解题思路
1. 暴力求解
**遇事不要慌,先暴力求解试试看,万一就过了呢,皆大欢喜**
当前本题过不了、、、、、、
没办法只能优化了,先说说暴力的思路,我们需要求最大面积,面积需要什么高 和 宽,那么我们直接枚举数组中所有的高和宽,然后搭配一下保存最大面积
- 枚举宽
我们固定每个数组元素为每个矩形的高,然后遍历数组,寻找每个矩形高能构成的最大面积,当左右两边第一次出现比当前高小的元素值,即为当前高能构成的最大值,每次保存最大值
- 枚举高
我们固定左右两边的长度即固定宽的长度,然后遍历数组,寻找当前长度中高最短的元素,即当前宽能构成的最大矩形,每次保存最大值
2. 单调栈
通过暴力解法,过不了,因为在枚举宽的同时需要寻找高,在枚举高的时候又要寻找宽,时间消耗非常大
那么可以利用递增栈优化暴力暴力求解的过程
- 当元素大于栈顶元素时,入栈
- 当元素小于栈顶元素时,维护栈的递增性,将小于当前元素的栈顶元素弹出,并计算面积
3. 单调栈+哨兵
在上面递增栈中,我们总是需要判断当前元素是否为最后元素或者为栈顶元素,很麻烦,那么可以在数组前后加上两个哨兵,充当坏点,在实际计算中不影响结果,但是简化我们的逻辑,正如我们高中或者初中学过的辅助线或者凑配法都是差不多的思路
具体实现看代码,注释超级详细
代码
1. 暴力求解
//枚举高
int largestRectangleArea(int* heights, int heightsSize){
int max = INT_MIN;//记录最大面积
for(int left = 0; left < heightsSize; left++)//左边宽
{
int minh = INT_MAX;
for(int right = left; right < heightsSize; right++)//右边宽
{
minh = fmin(minh, heights[right]);//取最小高
max = fmax(max, (right-left+1) * minh);//保存最大构成面积
}
}
return max;
}
作者:xun-ge-v
链接:https://leetcode.cn/problems/largest-rectangle-in-histogram/solution/by-xun-ge-v-626m/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。//枚举宽
int largestRectangleArea(int* heights, int heightsSize){
int max = -1;//记录最大面积
for(int mid = 0; mid < heightsSize; mid++)//
{
int h = heights[mid];//记录当前位置高
int left = mid, right = mid;
while(left-1 >= 0 && heights[left-1] >= h)//寻找最大宽度
{
left--;
}
while(right+1 < heightsSize && heights[right+1] >= h)
{
right++;
}
max = fmax(max, (right-left+1) * h);//保存最大面积
}
return max;
}
作者:xun-ge-v
链接:https://leetcode.cn/problems/largest-rectangle-in-histogram/solution/by-xun-ge-v-626m/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
2. 单调栈
int largestRectangleArea(int* heights, int heightsSize)
{
int stack[heightsSize];//定义栈
int top = -1;
stack[++top] = 0;
int max = -1;
for(int i = 1; i < heightsSize; i++)//遍历数组
{
if(heights[i] >= heights[stack[top]])//入栈
{
stack[++top] = i;
}
else
{
while(top != -1 && heights[i] < heights[stack[top]])//出栈并计算面积,维护递增性,需要对小于的元素全部出栈
{
if(top-1 == -1)//最后一个栈顶元素,出栈计算面积需要包含一下前面和后面,因为矩形可以延伸,这里需要好好想一想
{
max = fmax(max, i * heights[stack[top]]);
}
else
{
max = fmax(max, (i - stack[top] + stack[top] - stack[top-1] - 1) * heights[stack[top]]);//栈中元素,计算面积与需要延伸,能延伸多长就延伸多长
}
--top;
}
stack[++top] = i;
}
}
while(top != -1)//数组元素全部遍历完了,单是栈还有元素,进行清空栈
{
if(top-1 == -1)
{
max = fmax(max, (heightsSize) * heights[stack[top]]);
}
else
{
max = fmax(max, (heightsSize - 1 - stack[top-1]) * heights[stack[top]]);
}
--top;
}
return max;
}
作者:xun-ge-v
链接:https://leetcode.cn/problems/largest-rectangle-in-histogram/solution/by-xun-ge-v-626m/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。3. 单调栈+哨兵
int largestRectangleArea(int* heights, int heightsSize)
{
int ans[heightsSize+2];//添加哨兵
ans[0] = 0;
ans[heightsSize+1] = 0;
for(int i = 0; i < heightsSize; i++)
{
ans[i+1] = heights[i];
}
int stack[heightsSize+2];//定义栈
int top = -1;
stack[++top] = 0;
int max = 0;
for(int i = 1; i < heightsSize+2; i++)
{
if(ans[i] >= ans[stack[top]])//入栈
{
stack[++top] = i;
}
else
{
while(ans[i] < ans[stack[top]])//出栈
{
max = fmax(max, (i - stack[top] + stack[top] - stack[top-1] - 1) * ans[stack[top]]);
--top;
}
stack[++top] = i;
}
}
return max;
}
作者:xun-ge-v
链接:https://leetcode.cn/problems/largest-rectangle-in-histogram/solution/by-xun-ge-v-626m/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。边栏推荐
- 把boot和APP一起烧录进MCU
- 5 基本引用类型
- 云存储硬核技术内幕——(11) 女子会所的秘密
- Summary of some pytorch knowledge points that have been updated for a long time
- What is the difference between member variable and local variable
- js判断一个对象是否在一个对象数组中
- dot net double 数组转 float 数组
- 界面组件DevExpress ASP.NET Core v22.1 - 增强数据导出功能
- 2022-08-04日报:量化细胞内的信息流:机器学习时代下的研究进展
- flink cdc怎么指定位点,从某个位点开始消费mysql的Binlog?
猜你喜欢

为什么Redis默认序列化器处理之后的key会带有乱码?

界面组件DevExpress ASP.NET Core v22.1 - 增强数据导出功能

数据分析入门导读

DocuWare Platform - Content Services and Workflow Automation Platform for Document Management (Part 1)

A detailed explanation of what is software deployment

An article to answer what is the product library of the DevOps platform

面了三十个人,说说真实感受

分支控制if-else

Xi'an Zongheng Information × JNPF: Adapt to the characteristics of Chinese enterprises, fully integrate the cost management and control system

什么是 DevOps?看这一篇就够了!
随机推荐
你一定从未看过如此通俗易懂的YOLO系列(从v1到v5)模型解读
MySQL select加锁分析
2022年7月国产数据库大事记-墨天轮
In action: 10 ways to implement delayed tasks, with code!
如何实时监控销售数据?销售看板来帮你!
全球电子产品需求放缓,三星手机越南工厂每周只需要干 3~4 天
Projector reached the party benefits 】 【 beginners entry - brightness projection and curtain selection - from entry to the master
postman “header“:{“retCode“:“999999“
学 Go,最常用的技能是什么?打日志
功率放大器的设计要点
录音文件识别
张乐:研发效能的黄金三角及需求与敏捷协作领域的实践
##ansible自动化运维架构与简介
重构指标之如何监控代码圈复杂度
Go 言 Go 语,一文看懂 Go 语言文件操作
IP报文头解析
Li Mu's deep learning notes are here!
The electromagnetic compatibility EMC protection study notes
视频字幕API接口文档
js判断一个对象是否在一个对象数组中