当前位置:网站首页>leetcode单调栈经典例题——最大矩形
leetcode单调栈经典例题——最大矩形
2022-08-04 09:03:00 【你食不食油饼】
题目描述:


思路:写这道题之前,如果大家没有写过leetcode的84.柱状图中的最大矩形,大家可以移步看看
柱状图的最大矩形,把图中的1看作墙;
好了言归正传,看到这道题我相信大部分同学脑袋是蒙的,有点不知道如何下手的感觉,同学们不要慌,我们现在先看一下题目给出的这道例题,我们先不看原图,先看看这个怎么解

假设有一道题要我们求这个矩形只包含1的最大面积,大家会怎么写?

假设要我们求这个矩形呢?

又或者是这个矩形呢?相信大家看到这一定会有点思路,是的,我们求这个大矩形的最大面积就得把他高=1,高=2,高=3,高=4的时候的最大面积全计算出来,再进行比较,因为我们是不知道哪种情况下它具有最大面积的,所以我们只需要把这个二维数组转化为四个一维数组,再分别计算四个一维数组构成的矩形的最大面积!
现在知道为什么有必要去看一看那道 柱状图的最大矩形 了吧,这道题完全就是它的变种,我们拆分开后就可以利用单调栈去解决每一个矩形的最大面积!
注:这道单调栈仍旧可以加入哨兵,省略我们判断栈是否为空以及的步骤,以及出现遍历一次完成,栈中还有元素的情况!
进入代码:
public int maximalRectangle(char[][] matrix) {
int len = matrix[0].length;
int[] heights = new int[len + 2];
heights[0] = -1; //前置哨兵
int max = 0;
heights[len + 1] = -1; //后置哨兵
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < len; j++) {
if (matrix[i][j] == '1') heights[j + 1] += 1
else heights[j + 1] = 0;
}
max = Math.max(max,maxArea(heights));
}
return max;
}
public int maxArea(int[] heights){
Stack<Integer> stack = new Stack<>();
stack.push(0);
int max = 0;
for (int i = 1; i < heights.length; i++) {
while (heights[i] < heights[stack.peek()]){
Integer curHeight = heights[stack.pop()];
if (curHeight == 0) break;
int width = i - stack.peek() - 1;
max = Math.max(max,width * curHeight);
}
stack.push(i);
}
return max;
}总结:又是一道经典的单调栈例题,这道题逻辑不难,难的是我们想到利用大矩形拆分成一个一个小矩形的思想
边栏推荐
- How to import data from PG to kingbaseES
- Linux Redis cache avalanche, breakdown, penetration
- 优炫数据库只有数据文件如何恢复
- Yolov5 replaces the backbone network of "Megvii Lightweight Convolutional Neural Network ShuffleNetv2"
- 菲沃泰科创板上市:市值123亿 宗坚赵静艳夫妇身价76亿
- The separation configuration Libpq is supported, speaking, reading and writing
- 技术实现 | 图像检索及其在高德的应用
- ShuffleNet v2 network structure reproduction (Pytorch version)
- Unity3D data encryption
- 【STM32】STM32F103系列名称与封装、内存
猜你喜欢
![Detailed explanation of MSTP protocol configuration on Layer 3 switches [Huawei eNSP experiment]](/img/97/6c3662ef36b02bc42eec95abaa6bc5.png)
Detailed explanation of MSTP protocol configuration on Layer 3 switches [Huawei eNSP experiment]

交换机链路聚合详解【华为eNSP】

C Language Lectures from Scratch Part 6: Structure
![[STM32] STM32F103 series name and package, memory](/img/01/073f970c8c05ad24f976b26790ba61.png)
[STM32] STM32F103 series name and package, memory

PD 源码分析- Checker: region 健康卫士

JSP基本语法

记录十条工作中便利的API小技巧
![Detailed explanation of telnet remote login aaa mode [Huawei eNSP]](/img/cf/aaf3a0b794b1076423fc5b90ecc9f0.png)
Detailed explanation of telnet remote login aaa mode [Huawei eNSP]

After four years of outsourcing, the autumn recruits finally landed

DeLighT:深度和轻量化的Transformer
随机推荐
ShowMeAI —— Show u 三连
【STM32】STM32F103系列名称与封装、内存
LVGL's multi-language conversion tool -- a good assistant for font settings
三层交换机/路由器OSPF配置详解【华为eNSP实验】
[Cloud Residency Co-Creation] HCSD Celebrity Live Streaming – Employment Guide
[Punctuality Atom STM32 Serial] Chapter 4 STM32 First Experience Excerpted from [Punctual Atom] MiniPro STM32H750 Development Guide_V1.1
How to restore the Youxuan database with only data files
【云驻共创】HCSD 大咖直播–就业指南
MATLAB绘图总结
RL学习笔记(一)
思想茶叶蛋 (Jul 31,2022)| 元宇宙(Metaverse)下了一枚什么样的蛋
PD 源码分析- Checker: region 健康卫士
阿里云的数据库系统怎么升级更新的www.zgysffm.com怎么加快访问速度?
No module named 'flask_misaka' has been resolved [BUG solution]
Shared_preload_libraries cause many syntaxes not supported
优炫数据库只有数据文件如何恢复
94后字节P7晒出工资单:狠补了这个,真不错...
yolo x 跑起来,详细的不行,且内含800错误解决办法
ZbxTable 2.0 重磅发布!6大主要优化功能!
Grafana9.0发布,Prometheus和Loki查询生成器、全新导航、热图面板等新功能!