当前位置:网站首页>Force buckle monotone stack
Force buckle monotone stack
2022-07-23 15:08:00 【Love and hard reality】
Monotonic stack
The maximum rectangular area of the histogram


Ideas
Find the maximum rectangular area , You can get the positions of the left and right columns that are less than the current column height , Take the distance between the two boundary columns as the width , The current column is the height, and the maximum area of the lowest column with the current column is obtained .
Use a stack to store monotonically increasing data subscripts , When a decreasing value is encountered, the stack calculation area operation is performed .
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 The largest rectangle


Ideas
Each layer can be regarded as a histogram from top to bottom , Solve the maximum rectangular area of the histogram by monotone stack .
class Solution {
public int maximalRectangle(char[][] matrix) {
/* For each row, calculate the continuous 1 The number of , Then apply it The same algorithm of the maximum histogram solves the maximum area ( Monotonic stack ) */
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++) {
// Find the continuity above each grid 1 Number ( Include this case )
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;
}
}
边栏推荐
- Use of KOA framework
- Building personal network disk based on nextcloud
- 如何实现多个传感器与西门子PLC之间485无线通讯?
- The accuracy of digital addition
- The self-developed data products have been iterated for more than a year. Why not buy third-party commercial data platform products?
- [test platform development] 21. complete sending interface request and display response header information
- Subsequence --- edit distance
- MySQL unique index has no duplicate value, and the error is repeated
- 解决kotlin写Android项目编译报Execution failed for task ‘:app:kaptDebugKotlin‘.异常
- 直播课堂系统03补充-model类及实体
猜你喜欢

Linked list review!

基于双目相机拍摄图像的深度信息提取和目标测距matlab仿真

leetcode: 17. 电话号码的字母组合

直播课堂系统03补充-model类及实体

粒子边界碰撞的处理

基于matlab的CBOC信号调制解调仿真,输出其相关性,功率谱以及频偏跟踪

The best time to buy and sell stocks

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

基于matlab的BOC调制解调的同步性能仿真,输出跟踪曲线以及不同超前滞后码距下的鉴别曲线

多项式承诺Polynomial commitment方案汇总
随机推荐
dataframe.groupby学习资料
直播课堂系统02-搭建项目环境
Simulation of synchronization performance of BOC modulation and demodulation based on MATLAB, output tracking curve and identification curve under different lead lag code distance
[test platform development] 21. complete sending interface request and display response header information
什么是Promise?Promise有什么好处
Shell脚本案例---3
易基因|靶基因DNA甲基化测序(Target-BS)
MySQL 常用命令
Matlab simulation of Turbo code error rate performance
js拖拽元素
深度学习单图三维人脸重建
解决kotlin写Android项目编译报Execution failed for task ‘:app:kaptDebugKotlin‘.异常
面试官:生成订单30分钟未支付,则自动取消,该怎么实现?
动态规划-力扣
【启发式分治】启发式合并的逆思想
【无标题】测试【无标题】测试
[software testing] how to sort out your testing business
Leetcode: 17. letter combination of phone number
LeetCode-227-基本计算器||
152. Product maximum subarray