当前位置:网站首页>剑指 Offer II 039. 直方图最大矩形面积
剑指 Offer II 039. 直方图最大矩形面积
2022-07-06 05:14:00 【小白码上飞】
概要
单调栈,思考起来比较费劲。
题目
给定非负整数数组 heights ,数组中的数字用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。
求在该柱状图中,能够勾勒出来的矩形的最大面积。

链接:https://leetcode.cn/problems/0ynMMM
思路
还是使用单调栈。你会发现,这几道题的核心,都是利用一个单调栈,之后在遍历数组时,让当前元素和栈顶元素做比较,一直保证栈顶元素是最大(或最小)。
解答这一道题的几个核心点如下:
- 以一个柱的高度为中心,向两边扩散找边界。找到比这个柱子矮的柱子,就是边界。这时就可以根据柱的高度和边界长度计算面积了。
- 用一个栈顶大于栈底的单调栈,来维护左边界。
- 如果遍历到的当前元素大于栈顶元素,则入栈。
- 如果遍历到的当前元素小于栈顶元素,则栈顶元素则为柱,而当前元素的位置,就是最右端的边界。而因为是单调栈,所以栈顶下面的元素,则是栈顶为柱时,最右端的边界。
- 将栈顶出栈,比较新的栈顶和当前元素的大小关系,重复前两条的逻辑
- 遍历完毕,再将栈中还存在的元素,做进一步计算。只有最右边的柱高于前面的柱时,才会出现这种情况。因此此时的右边界,就是数组的右边界。
解法:单调栈
代码
public int largestRectangleArea(int[] heights) {
Deque<Integer> stack = new ArrayDeque<>();
// 方便处理边界问题
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) {
// 获取栈顶元素下标的同时移除了栈顶元素
int height = heights[stack.pop()];
int with = i - stack.peek() - 1;
result = Math.max(result, height * with);
}
stack.push(i);
}
// 处理栈中剩余的边界。此时右边界必然是数组的最右端
while (stack.peek()!=-1) {
result = Math.max(result, heights[stack.pop()] * (heights.length - stack.peek() - 1));
}
return result;
}
提交结果

P.S.这个题解的动画很棒:https://leetcode.cn/problems/0ynMMM/solution/hua-luo-yue-que-wo-zhen-de-zhen-de-nu-li-ohjt/
边栏推荐
- 2021robocom robot developer competition (Preliminary)
- 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
- Class inheritance in yyds dry inventory C
- Summary of three log knowledge points of MySQL
- Talking about the type and function of lens filter
- Chip debugging of es8316 of imx8mp
- Postman前置脚本-全局变量和环境变量
- Zynq learning notes (3) - partial reconfiguration
- Three. JS learning - light and shadow (understanding)
- Upload nestjs configuration files, configure the use of middleware and pipelines
猜你喜欢

GAMES202-WebGL中shader的編譯和連接(了解向)

GAMES202-WebGL中shader的编译和连接(了解向)

Figure database ongdb release v-1.0.3

Hyperledger Fabric2. Some basic concepts of X (1)

Codeforces Round #804 (Div. 2) Editorial(A-B)
![[classic example] binary tree recursive structure classic topic collection @ binary tree](/img/39/0319c4be43716f927b9d98d89f7655.jpg)
[classic example] binary tree recursive structure classic topic collection @ binary tree

Zynq learning notes (3) - partial reconfiguration

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

Compilation and connection of shader in games202 webgl (learn from)

Leetcode dynamic planning day 16
随机推荐
Yyds dry inventory SSH Remote Connection introduction
Acwing week 58
Lepton 无损压缩原理及性能分析
Microblogging hot search stock selection strategy
[noip2008 improvement group] stupid monkey
Some common skills on unity inspector are generally used for editor extension or others
pix2pix:使用条件对抗网络的图像到图像转换
用StopWatch 统计代码耗时
Select knowledge points of structure
Configuration file converted from Excel to Lua
集合详解之 Map + 面试题
Force buckle 1189 Maximum number of "balloons"
ByteDance program yuan teaches you how to brush algorithm questions: I'm not afraid of the interviewer tearing the code
Please wait while Jenkins is getting ready to work
Huawei equipment is configured with OSPF and BFD linkage
Modbus protocol communication exception
The video in win10 computer system does not display thumbnails
Codeforces Round #804 (Div. 2)
Biscuits (examination version)
麦斯克电子IPO被终止:曾拟募资8亿 河南资产是股东