问题描述:

  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的更多相关文章

  1. 【LeetCode】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址: https://leetc ...

  2. 【LeetCode】84. Largest Rectangle in Histogram

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

  3. 【LeetCode】84. Largest Rectangle in Histogram——直方图最大面积

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  4. 【LeetCode】764. Largest Plus Sign 解题报告(Python)

    [LeetCode]764. Largest Plus Sign 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...

  5. 【LeetCode】813. Largest Sum of Averages 解题报告(Python)

    [LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  6. 【LeetCode】深搜DFS(共85题)

    [98]Validate Binary Search Tree [99]Recover Binary Search Tree [100]Same Tree [101]Symmetric Tree [1 ...

  7. 【一天一道LeetCode】#84. Largest Rectangle in Histogram

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given n ...

  8. 【LeetCode】084. Largest Rectangle in Histogram

    题目: Given n non-negative integers representing the histogram's bar height where the width of each ba ...

  9. 【Leetcode】179. Largest Number

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

随机推荐

  1. Authors and instutes in MCT

    Authors and instutes in MCT Table of Contents 1. Authors and Institutes 1.1. Industry 1 Authors and ...

  2. 52. spring boot日志升级篇—log4j多环境不同日志级别的控制【从零开始学Spring Boot】

    在上一章节中我们介绍了,仅通过log4j-spring.properties对日志级别进行控制,对于需要多环境部署的环境不是很方便,可能我们在开发环境大部分模块需要采用DEBUG级别,在测试环境可能需 ...

  3. resin4开启jmx

    https://blog.csdn.net/liuxiao723846/article/details/51321010 https://blog.csdn.net/u010342008/articl ...

  4. poj——1330 Nearest Common Ancestors

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30082   Accept ...

  5. [bzoj1293][SCOI2009]生日礼物(单调队列)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1293 分析: 问题的关键就是选择每种颜色的哪一个好.可以先把每种颜色的第一个一起,更新 ...

  6. Oracle Multitenant Environment (三) Plan for a cdb

    Below tables contains contant you need to consider while planning for a CDB. Action Considerations f ...

  7. 【CV知识学习】Fisher Vector

    在论文<action recognition with improved trajectories>中看到fisher vector,所以学习一下.但网上很多的资料我觉得都写的不好,查了一 ...

  8. Android自己定义控件系列案例【五】

    案例效果: 案例分析: 在开发银行相关client的时候或者开发在线支付相关client的时候常常要求用户绑定银行卡,当中银行卡号一般须要空格分隔显示.最常见的就是每4位数以空格进行分隔.以方便用户实 ...

  9. 百练1088:滑雪 【DP】+【DFS】

    总Time Limit: 1000ms Memory Limit: 65536kB Description Michael喜欢滑雪百这并不奇怪, 由于滑雪的确非常刺激.但是为了获得速度,滑的区域必须向 ...

  10. webrtc 视频 demo

    webrtc 视频 demo webrtc网上封装的很多,demo很多都是一个页面里实现的,今天实现了个完整的 , A 发视频给 B A webrtc.html作为offer <!DOCTYPE ...