【Leetcode】84. Largest Rectangle in Histogram 85. Maximal Rectangle
问题描述:
84:直方图最大面积。
85:0,1矩阵最大全1子矩阵面积。

问题分析:
对于84,如果高度递增的话,那么OK没有问题,不断添加到栈里,最后一起算面积(当然,面积等于高度h * disPos,这里的高度取决于两个h的较小者,那么如果有序的话,只需要计算每一个最小值h,在用到当前位置与最小值h所在位置的差值,也就是柱子数目,就OK了)。如果新h[i]的高度比之前的高度小的话,那么就可以认为之前所有比h[i]高的柱子都需要计算一下面积,然后对于破坏递增规则的h[i]会继续加入栈中。为甚会酱婶儿呢,因为你想哈,如果h[i]是最大矩形的右边界那么h[i + 1]是不一定有:h[i]>h[i+ 1]。哎呀,不用想了,肯定是对的 ,如果h[i]<h[i+ 1],那我直接用i+1的柱子当右边界的话,面积肯定会更大的嘛!没错,这就是我们为什么会用一个数据结构来说维护一个递增的序列来求解问题的原因。
对于85,我说可以把他转换成84的求解形式,你信不信啊?不信的话,请看:
对于下面这个矩阵:
1 0 1 1 1
1 1 1 1 1
1 0 1 1 0
1 1 1 1 1
我们用一个数组height[]来计算当前row下,每一column之前row(i -> 0)有多少个连续的1。
row 0: 1 0 1 1 1
row 1: 2 1 2 2 2
row 2: 3 0 3 3 0
row 3: 4 1 4 4 1
我们对于每一行 row 来说,这个数字不就是表示的是当横轴在row处,直方图的高度吗!神奇,这样我们对于每一行都执行84中的算法,是不是就可以得到全局面积最大值了。
问题解决:
实现上使用stack来维护递增height,没有问题的。这里提前将原来的数组height压入了0,使得最后一定会使stack中的数弹出来。如果不压入,那么最后记得check一下stack也是可以的。
代码如下:
class Solution {
public:
int maximalRectangle(vector<vector<char>>& matrix) {
if(matrix.size() == ) return ;
int n = matrix.size(), m = matrix[].size();
stack<int>sta;
int maxArea = ;
vector<int>height(m + , );
for(int i = ; i < n; i ++){
for(int j = ; j < m; j ++){
if(matrix[i][j] == '')
height[j] ++;
else
height[j] = ;
}
while(!sta.empty()) sta.pop();
for(int j = ; j < height.size(); j ++){
while( !sta.empty() && height[j] < height[sta.top()]){
int h = height[sta.top()];
sta.pop();
int idLeft = sta.size() > ? sta.top(): -;
maxArea = max(maxArea, h * (j - idLeft - ));
}
sta.push(j);
}
}
return maxArea;
}
};
PS: celebration , leetcode今天做完这两道题,已经干掉整100题了。不信? 哈哈 :

继续加油,别装逼~。~
【Leetcode】84. Largest Rectangle in Histogram 85. Maximal Rectangle的更多相关文章
- 【LeetCode】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址: https://leetc ...
- 【LeetCode】84. Largest Rectangle in Histogram
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- 【LeetCode】84. Largest Rectangle in Histogram——直方图最大面积
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- 【LeetCode】764. Largest Plus Sign 解题报告(Python)
[LeetCode]764. Largest Plus Sign 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...
- 【LeetCode】813. Largest Sum of Averages 解题报告(Python)
[LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】深搜DFS(共85题)
[98]Validate Binary Search Tree [99]Recover Binary Search Tree [100]Same Tree [101]Symmetric Tree [1 ...
- 【一天一道LeetCode】#84. Largest Rectangle in Histogram
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given n ...
- 【LeetCode】084. Largest Rectangle in Histogram
题目: Given n non-negative integers representing the histogram's bar height where the width of each ba ...
- 【Leetcode】179. Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
随机推荐
- codeforces 689 Mike and Shortcuts(最短路)
codeforces 689 Mike and Shortcuts(最短路) 原题 任意两点的距离是序号差,那么相邻点之间建边即可,同时加上题目提供的边 跑一遍dijkstra可得1点到每个点的最短路 ...
- dp专题备忘录
hdu 1024:基础dp题 hdu 1029:主元素问题,很快的解法,计数器 hdu 1069:LIS hdu 1074:数位dp,数位dp基础 hdu 1257:简单LIS,要仔细分析为什么是求最 ...
- PAT 1079. 延迟的回文数
PAT 1079. 延迟的回文数 给定一个 k+1 位的正整数 N,写成 ak...a1a0 的形式,其中对所有 i 有 0 <= ai < 10 且 ak > 0.N 被称为一个回 ...
- 【Codeforces 493C】Vasya and Basketball
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 枚举三分线(离散后)的位置 然后根据预处理的前缀和,快速算出两个队伍的分数. [代码] #include <bits/stdc++.h& ...
- gdb个人使用记录
参考博客:https://blog.csdn.net/zdy0_2004/article/details/80102076 安装gdb,查看版本确认成功: sudo apt install gdb g ...
- ZOJ 3201 树形背包问题
题目大意: 0~n-1号这n个点,每个点有个权值,由无向边形成了一棵树,希望在这棵树上找到一棵长为m的子树使总的权值最小 基本的树形背包问题 令dp[u][j] 表示u号节点对应子树中有j个节点所能得 ...
- Javaweb中文乱码问题
request.setCharacterEncoding("utf-8");必须写在获得参数之前,即request.getParameter();之前
- J - Invitation Cards 最短路
In the age of television, not many people attend theater performances. Antique Comedians of Malidine ...
- poj——2891 Strange Way to Express Integers
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 16839 ...
- ubuntu无法update
ubuntu系统执行sudo apt-get update报错解决方法: 编辑源列表文件 sudo vi /etc/apt/sources.list 将原来的列表删除,添加如下内容(中科大镜像源) d ...