47. Largest Rectangle in Histogram && Maximal Rectangle
Largest Rectangle in Histogram
Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.
Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]
.
The largest rectangle is shown in the shaded area, which has area = 10
unit.
For example, Given height = [2,1,5,6,2,3]
, return 10
.
思路: 注意一点: 只要计算出以每个柱形为最小值的矩形面积即可。使用一个栈,栈内始终保存一个递增序列的 index,若是 新的柱形长度小于栈顶元素,则退出栈顶直到栈内元素的长度不大于新的柱形的长度为止,并且,对于每一个退栈元素,计算以其长度为最小值的面积。(宽的左边为其自身位置,右边为新到元素的位置)时间:O(n)
class Solution {
public:
int largestRectangleArea(vector<int> &height) {
int n = height.size(), max_area = 0;
int Id, area;
int i = 0;
stack<int> st; // save the index
while(i < n) {
if(st.empty() || height[i] >= height[st.top()]) st.push(i++);
else {
Id = st.top();
st.pop();
area = height[Id] * (st.empty() ? i : i - st.top() - 1);
if(area > max_area) max_area = area;
}
}
while(!st.empty()) {
Id = st.top();
st.pop();
area = height[Id] * (st.empty() ? i : i - st.top() - 1);
if(area > max_area) max_area = area;
}
return max_area;
}
};
Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.
思路: 一行一行的记录下当前高度, 用上题的思路计算一下,即可。时间: O(n2)
int getMaxArea(vector<int> &h) {
stack<int> st;
int maxArea, i = 0;
while(i < h.size()) {
if(st.empty() || h[st.top()] <= h[i]) { st.push(i++); continue; }
while(!st.empty() && h[st.top()] > h[i]) {
int id = st.top();
st.pop();
int area = h[id] * (st.empty() ? i : i - st.top() - 1);
if(area > maxArea) maxArea = area;
}
}
while(!st.empty()) {
int id = st.top();
st.pop();
int area = h[id] * (st.empty() ? i : i - st.top() - 1);
if(area > maxArea) maxArea = area;
}
return maxArea;
}
class Solution {
public:
int maximalRectangle(vector<vector<char> > &matrix) {
if(matrix.size() == 0 || matrix[0].size() == 0) return 0;
int row = matrix.size(), col = matrix[0].size();
vector<int> h(row, 0);
int maxArea = 0;
for(int c = 0; c < col; ++c) {
for(int r = 0; r < row; ++r) {
if(matrix[r][c] == '1') ++h[r];
else h[r] = 0;
}
maxArea = max(maxArea, getMaxArea);
}
return maxArea;
}
};
47. Largest Rectangle in Histogram && Maximal Rectangle的更多相关文章
- LeetCode之“动态规划”:Maximal Square && Largest Rectangle in Histogram && Maximal Rectangle
1. Maximal Square 题目链接 题目要求: Given a 2D binary matrix filled with 0's and 1's, find the largest squa ...
- leetcode@ [84/85] Largest Rectangle in Histogram & Maximal Rectangle
https://leetcode.com/problems/largest-rectangle-in-histogram/ https://leetcode.com/problems/maximal- ...
- 84. Largest Rectangle in Histogram *HARD* -- 柱状图求最大面积 85. Maximal Rectangle *HARD* -- 求01矩阵中的最大矩形
1. Given n non-negative integers representing the histogram's bar height where the width of each bar ...
- Maximal Rectangle&Largest Rectangle in Histogram
这两天在做leetcode的题目,最大矩形的题目以前遇到很多次了,一直都是用最笨的方法,扫描每个柱子,变换宽度,计算矩形面积,一直都以为就这样O(n2)的方法了,没有想到居然还有研究出了O(n)的算法 ...
- [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- LeetCode 84--柱状图中最大的矩形( Largest Rectangle in Histogram) 85--最大矩形(Maximal Rectangle)
84题和85五题 基本是一样的,先说84题 84--柱状图中最大的矩形( Largest Rectangle in Histogram) 思路很简单,通过循环,分别判断第 i 个柱子能够延展的长度le ...
- [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- 84. Largest Rectangle in Histogram
https://www.cnblogs.com/grandyang/p/4322653.html 1.存储一个单调递增的栈 2.如果你不加一个0进去,[1]这种情况就会输出结果0,而不是1 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 ...
随机推荐
- 初识selendroid
Testerhome社区的lihuazhang对selendroid官网的部分内容进行了翻译和讲解. 以下内容均摘自lihuazhang.感谢lihuazhang的讲解.原文地址:https://gi ...
- 在Eclipse上建立hbase 0.98.3/0.96.2源代码阅读环境
2.1. 切换到源代码目录,执行: mvn 黄色部分作用为设置代理.由于本人的编译环境在公司内网,所以需要设置代理 2.2. 生成eclipse项目环境: mvn eclipse:eclipse -D ...
- 64位 ubuntu android studio gradle 权限不够 缺少文件和权限导致
安装 32位 库文件 sudo apt-get install lib32z1 给文件夹加权限 chmod 777 -R SDK chmod 777 -R android-studio -R表示所有 ...
- 谷歌浏览器中安装.crx扩展名的离线Chrome插件
一.本地拖放安装 1.下载扩展程序/脚本程序至本地计算机: 2.将其直接拖拽到浏览器的“扩展程序”(chrome://chrome/extensions/)页面. 二.解决“只能通过Chrome网上应 ...
- iOS ShareSDK 使用
流量精灵软件中,也在大部分地方使用到了shareSDK 这个三方开源库.具体的有两种需求 a.弹出所有分享模块 b.只弹出指定的平台:如微信朋友圈和QQ . 配置方法,三方库中也很详细,这里我只有写出 ...
- 数据库对象映射为java对象,不使用框架
方法: public static <T> List<T> processResultSetToList(ResultSet rs, Class<T> clazz) ...
- 团队开发——冲刺1.e
冲刺阶段一(第五天) 冲刺阶段一(第五天) 1.昨天做了什么?优化界面细节. 查看C#资料,再解决自己电脑的问题. 2.今天准备做什么? 为解决自己电脑的问题,查找关于C#的资料,后期做准备.
- HDU5000 (DP + 规律)
题意:举例子好说点,告诉你4个数字,8,6,4,2四个数字,组成一个四位数,如果两个数字分别是1111,2222,则2222会吧1111杀掉,就是组成的四位数不能每一位都小于或等于一个数,然后让你求出 ...
- PCL中point cloud的数据类型
出处: http://wiki.ros.org/pcl/Overview 1.数据类型 1.1 ROS中point cloud数据类型 sensor mesgs::PointCloud sensor ...
- Smart210学习记录----nand flash驱动
[详解]如何编写Linux下Nand Flash驱动 :http://www.cnblogs.com/linux-rookie/articles/3016990.html 当读写文件请求到来的时候, ...