当前位置:网站首页>力扣-单调栈
力扣-单调栈
2022-07-23 09:56:00 【情、狠现实】
单调栈
直方图的最大矩形面积


思路
寻找最大矩形面积,可以获取小于当前柱子高度的左右两个柱子位置,以这两个边界柱子距离为宽,当前柱子为高求得以当前柱子为最低柱子的最大面积。
使用一个栈存储单调递增的数据下标,当遇到递减值时则执行出栈计算面积操作。
class Solution {
public int largestRectangleArea(int[] heights) {
LinkedList<Integer> li = new LinkedList<>();
int max = 0;
li.push(-1);
for (int i = 0; i < heights.length; i++) {
while(li.peek() != -1 && heights[li.peek()] > heights[i]) {
max = Math.max(max, heights[li.pop()] * (i - li.peek() - 1));
}
li.push(i);
}
while(li.peek() != -1) {
max = Math.max(max, heights[li.pop()] * (heights.length - li.peek() - 1));
}
return max;
}
}
85 最大矩形


思路
可以从上往下以每一层作为底边看做一个直方图,通过单调栈对该直方图求解最大矩形面积。
class Solution {
public int maximalRectangle(char[][] matrix) {
/* 对于每一行计算其上连续1的个数, 然后对其应用 最大直方图的相同算法求解最大面积(单调栈) */
final int rows = matrix.length;
final int cols = matrix[0].length;
int[][] m = new int[rows][cols];
LinkedList<Integer> s = new LinkedList<>();
s.push(-1);
int max = 0;
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
// 求出每格子上方连续1个数(包含本格)
if (matrix[r][c] == '1') {
m[r][c] = 1;
}
if (r != 0 && m[r][c] == 1) {
m[r][c] += m[r - 1][c];
}
while(s.peek() != -1 && m[r][c] < m[r][s.peek()]) {
max = Math.max(max, m[r][s.pop()] * (c - s.peek() - 1));
}
s.push(c);
}
while(s.peek() != -1) {
max = Math.max(max, m[r][s.pop()] * (cols - s.peek() - 1));
}
}
return max;
}
}
边栏推荐
- 深度学习单图三维人脸重建
- js判断元素是否到滚动到顶部
- Official wechat product! Applet automation framework minium sharing Preview
- C language project practice: 24 point game calculator (based on knowledge points such as structure, pointer, function, array, loop, etc.)
- 一道代码题看 CommonJS 模块化规范
- turbo编译码误码率性能matlab仿真
- Deep learning single image 3D face reconstruction
- linux定时备份数据库脚本
- 452. 用最少数量的箭引爆气球
- String function of MySQL function summary
猜你喜欢

OpenHarmony南向学习笔记——Hi3861+HC-SR04超声波检测
![[untitled] test [untitled] test](/img/9d/c80dd9a1df2cd6cbbfc597d73a63b2.png)
[untitled] test [untitled] test

mysql函数汇总之字符串函数
![[test platform development] 23. interface assertion function - save interface assertion and edit echo](/img/36/aed4feb6a4e40b6b5c06206a8ed440.png)
[test platform development] 23. interface assertion function - save interface assertion and edit echo

Openharmony South learning notes - hi3861+hc-sr04 ultrasonic testing

爬虫中selenium实现自动给csdn博主文章点收藏

直播课堂系统02-搭建项目环境

面试官:生成订单30分钟未支付,则自动取消,该怎么实现?

General of MySQL_ Log log

Use of KOA framework
随机推荐
[untitled]
Zhongwang CAD professional 2022 software installation package download and installation tutorial
NVIDIA vid2vid paper reproduction
494. Objectives and
581. Shortest unordered continuous subarray
21 - vertical traversal of binary tree
Building personal network disk based on nextcloud
Live classroom system 01 database table design
String function of MySQL function summary
mysql 之general_log日志
452. Detonate the balloon with the minimum number of arrows
Monotonous stack!!!
Mathematical function of MySQL function summary
c语言:深度刨析const关键字
[applet automation minium] i. framework introduction and environment construction
[array & String & Macro exercise]
自研的数据产品迭代了一年多,为什么不买第三方商业数据平台产品呢?
Fastapi application joins Nacos
Head pose estimation principle and visualization_ Loveliuzz's blog - Programmer's Homestead_ Head posture estimation
[software testing] how to sort out your testing business