当前位置:网站首页>单调栈-84. 柱状图中最大的矩形
单调栈-84. 柱状图中最大的矩形
2022-07-03 08:24:00 【later_rql】
1.题目
本题与42. 接雨水基本一致,建议两个结合着做
给定 n 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。
求在该柱状图中,能够勾勒出来的矩形的最大面积。
示例 1:

输入:heights = [2,1,5,6,2,3]
输出:10
解释:最大的矩形为图中红色区域,面积为 10
示例 2:

输入: heights = [2,4]
输出: 4
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/largest-rectangle-in-histogram
2.解题思路
解法一:动态规划
通过题目理解,是要求最大的两个柱子(也可能是单个柱子)的最大面积,可以考虑用动态规划来做
和42.接雨水那题有些类似,不同点在于,该题需要寻找当前柱子左边和右边小于其的柱子的索引,因此需要记录的是索引

方法二:单调栈
本题和42.接雨水的题目对应,接雨水是要求栈内元素从大到小排序,然后,取栈顶和栈顶的下一个元素以及要入栈的三个元素来接水!而本题是要求栈内元素从小到大排序,同样,栈顶和栈顶的下一个元素以及要入栈的三个元素来接水!

然后分析三种情况:
剩下就是分析清楚如下三种情况:
- 情况一:当前遍历的元素heights[i]小于栈顶元素heights[st.top()]的情况(出现最大面积)
- 情况二:当前遍历的元素heights[i]等于栈顶元素heights[st.top()]的情况
- 情况三:当前遍历的元素heights[i]大于栈顶元素heights[st.top()]的情况
3.代码
方法一:动态规划
public int largestRectangleArea(int[] heights) {
int length = heights.length;
int[] minLeftIndex = new int [length];
int[] maxRigthIndex = new int [length];
// 记录左边第一个小于该柱子的下标
minLeftIndex[0] = -1 ;
for (int i = 1; i < length; i++) {
int t = i - 1;
// 这里不是用if,而是不断向右寻找的过程
while (t >= 0 && heights[t] >= heights[i]) t = minLeftIndex[t];
minLeftIndex[i] = t;
}
// 记录每个柱子 右边第一个小于该柱子的下标
maxRigthIndex[length - 1] = length;
for (int i = length - 2; i >= 0; i--) {
int t = i + 1;
while(t < length && heights[t] >= heights[i]) t = maxRigthIndex[t];
maxRigthIndex[i] = t;
}
// 求和
int result = 0;
for (int i = 0; i < length; i++) {
int sum = heights[i] * (maxRigthIndex[i] - minLeftIndex[i] - 1);
result = Math.max(sum, result);
}
return result;
}
方法二:单调栈
int largestRectangleArea(int[] heights) {
Stack<Integer> st = new Stack<Integer>();
// 数组扩容,在头和尾各加入一个元素
int [] newHeights = new int[heights.length + 2];
newHeights[0] = 0;
newHeights[newHeights.length - 1] = 0;
for (int index = 0; index < heights.length; index++){
newHeights[index + 1] = heights[index];
}
heights = newHeights;
st.push(0);
int result = 0;
// 第一个元素已经入栈,从下标1开始
for (int i = 1; i < heights.length; i++) {
// 注意heights[i] 是和heights[st.top()] 比较 ,st.top()是下标
if (heights[i] > heights[st.peek()]) {
st.push(i);
} else if (heights[i] == heights[st.peek()]) {
st.pop(); // 这个可以加,可以不加,效果一样,思路不同
st.push(i);
} else {
while (heights[i] < heights[st.peek()]) {
// 注意是while
int mid = st.peek();
st.pop();
int left = st.peek();
int right = i;
int w = right - left - 1;
int h = heights[mid];
result = Math.max(result, w * h);
}
st.push(i);
}
}
return result;
4.性能
方法一:动态规划
- 时间复杂度:O(n)
- 空间复杂度:O(n)
方法二:单调栈
- 时间复杂度:O(n)
- 空间复杂度:O(n)
边栏推荐
- Osganimation library parsing
- Chocolate installation
- KunlunBase MeetUP 等您来!
- Creation and content of mapnode -- osgearth rendering engine series (2)
- C语言-入门-精华版-带你走进编程(一)
- Golang 时间格式整理
- Visual Studio (VS) shortcut keys
- Mxone Pro adaptive 2.0 film and television template watermelon video theme apple cmsv10 template
- One dimensional array two dimensional array (sort Max insert sort)
- Swagger document configuration
猜你喜欢

Three characteristics

About Wireshark's unsuccessful installation of npcap

Get to know unity2 for the first time
![[cloud native] introduction and use of feign of microservices](/img/39/05cf7673155954c90e75a8a2eecd96.jpg)
[cloud native] introduction and use of feign of microservices

C#课程设计之学生教务管理系统

Unity editor expansion - controls, layouts

数据的存储

UE4 source code reading_ Mobile synchronization

Osgearth target selection

Un système de gestion de centre commercial pour la conception de cours de technologie d'application de base de données
随机推荐
Explain sizeof, strlen, pointer, array and other combination questions in detail
Golang json格式和结构体相互转换
Storage of data
Golang's range
Unity Editor Extension - Outline
【云原生】微服务之Feign的介绍与使用
[K & R] Chinese Second Edition personal questions Chapter1
Jupyter remote server configuration and server startup
Transplantation of tslib Library
String class
Dealing with duplicate data in Excel with xlwings
[updating] wechat applet learning notes_ three
Unity Editor Extension - drag and drop
Compilation error: "not in executable format: file format not recognized"“
Conversion between string and int types in golang
Golang的range
animation
Unity editor expansion - controls, layouts
Introduction to hexadecimal coding
Unity learning notes