题目:

Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area.

For example, given the following matrix:

1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0

Return 6

 

题解:

  这个题是在 Largest Rectangle in Histogram上的延伸,二维矩阵每一行为底,都可以看做一个直方图,输入矩阵有多少行,就可以形成多少个直方图,循环调用直方图最大面积函数即可。

Solution

class Solution {
public:
int maximalRectangle(vector<vector<char>>& matrix) {
int res = ;
if(matrix.empty())
return res;
int n = matrix[].size();
vector<int> cals(n, );
for(int i = ; i < matrix.size(); ++i){
for(int j = ; j < n; ++j){
cals[j] = matrix[i][j] == '' ? : cals[j] + ;
}
res = max(res, maxRectangleArea(cals));
}
return res;
}
private:
int maxRectangleArea(vector<int> &nums){
int n = nums.size();
int res = , area = ;
nums.push_back();
stack<int> s;
for(int i = ; i <= n;){
if(s.empty() || nums[s.top()] <= nums[i])
s.push(i++);
else {
int cur = s.top(); s.pop();
area = nums[cur] * (s.empty() ? i : (i - s.top() - ));
res = max(res, area);
}
}
return res;
}
};

  left数组表示左边界是1的位置,right数组表示右边界是1的位置,那么对于任意一行的第j个位置,矩形为(right[j] - left[j]) * height[j]

Solution 2 动态规划

class Solution {
public:
int maximalRectangle(vector<vector<char>>& matrix) {
int res = ;
if(matrix.empty())
return res;
int n = matrix[].size();
vector<int> height(n, ), left(n, ), right(n, n);
for(int i = ; i < matrix.size(); ++i){
int l = , r = n;
for(int j = ; j < n; ++j){
if(matrix[i][j] == ''){
++height[j];
left[j] = max(left[j], l);
} else {
l = j + ;
height[j] = ;
left[j] = ;
right[j] = n;
}
}
for(int j = n - ; j >= ; --j){
if(matrix[i][j] == ''){
right[j] = min(right[j], r);
res = max(res, height[j] * (right[j] - left[j]));
} else {
r = j;
}
}
}
return res;
}
};

【LeetCode】085. Maximal Rectangle的更多相关文章

  1. 【LeetCode】85. Maximal Rectangle

    Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...

  2. 【leetcode】85. Maximal Rectangle(单调栈)

    Given a rows x cols binary matrix filled with 0's and 1's, find the largest rectangle containing onl ...

  3. 【LeetCode】85. Maximal Rectangle 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/maximal- ...

  4. 【一天一道LeetCode】#85. Maximal Rectangle

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

  5. 【Leetcode】84. Largest Rectangle in Histogram 85. Maximal Rectangle

    问题描述: 84:直方图最大面积. 85:0,1矩阵最大全1子矩阵面积. 问题分析: 对于84,如果高度递增的话,那么OK没有问题,不断添加到栈里,最后一起算面积(当然,面积等于高度h * disPo ...

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

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

  7. 【LeetCode】84. Largest Rectangle in Histogram

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

  8. 【leetcode】Contains Duplicate & Rectangle Area(easy)

    Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...

  9. 【LeetCode】221. Maximal Square

    Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...

随机推荐

  1. 【BZOJ3060】[Poi2012]Tour de Byteotia 并查集

    [BZOJ3060][Poi2012]Tour de Byteotia Description 给定一个n个点m条边的无向图,问最少删掉多少条边能使得编号小于等于k的点都不在环上. Input     ...

  2. Collective Mindsets (easy)(逻辑题)

    Collective Mindsets (easy) Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d &am ...

  3. Spring中的国际化资源以及视图跳转

    一.SpringMVC对国际化的支持 SpringMVC进行资源国际化主要是通过ResourceBundleMessageSource实现的,xml如下配置: <bean id="me ...

  4. 九度OJ 1181:遍历链表 (链表、排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2733 解决:1181 题目描述: 建立一个升序链表并遍历输出. 输入: 输入的每个案例中第一行包括1个整数:n(1<=n<=1 ...

  5. READ_TEXT

    [转自http://lz357502668.blog.163.com/blog/static/1649674320109119101907/]这里,定义ITAB内表来存储长文本,并放到内表ITAB_E ...

  6. 目标检测--之RCNN

    目标检测--之RCNN 前言,最近接触到的一个项目要用到目标检测,还有我的科研方向caption,都用到这个,最近电脑在windows下下载数据集,估计要一两天,也不能切换到ubuntu下撸代码~.所 ...

  7. 波浪分析数据转换:大智慧、钱龙、胜龙可用Advanced GET ToGet 数据转换器V3.05特别版

    http://www.55188.com/thread-4185427-1-1.html Advanced GET ToGet 数据转换器V3.05特别版,大智慧可用软件数据类型选“分析家”源软件数据 ...

  8. JavaWeb:前端开发基础

    JavaWeb:前端开发基础 内联元素和块级元素 说明: 联元素和块级元素都是html中的范畴,块元素和内联元素的主要差异是块元素是从新的一行开始.而内联元素一般显示在一行上.但是可以通过css的di ...

  9. iOS Code Signing: 解惑详解

    iPhone开发的代码签名 代码签名确保代码的真实以及明确识别代码的来源.在代码运行在一个开发系统以前,以及在代码提交到Apple发布以前,Apple要求所有的的应用程序都必须进行数字签名.另外,Ap ...

  10. linux安全相关

    2017-05-11突然谈到linux安全相关的话题,记录一下 搜了一下,找到一篇介绍apparmor和selinux的文章 http://www.361way.com/apparmor-selinu ...