题目:

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. HYSBZ - 1799 self 同类分布

    self 同类分布 HYSBZ - 1799 给出a,b,求出[a,b]中各位数字之和能整除原数的数的个数.Sample Input 10 19 Sample Output 3 Hint [约束条件] ...

  2. hive常规配置及常用命令使用

    hive 常用的几种shell交互方式 查看hive命令帮助:bin/hive -help [hd@hadoop-senior hive]$ bin/hive -help usage: hive -d ...

  3. Webpack探索【2】--- 安装、项目初始化、webpack.config.js配置文件

    本文主要讲安装.项目初始化.webpack.config.js配置文件方面的内容.

  4. html_dom类读取

    上传类文件以后,有三种方式调用这个类:从url中加载html文档从字符串中加载html文档从文件中加载html文档 复制代码 代码如下: <?php// 新建一个Dom实例$html = new ...

  5. Navicat试用期破解方法(转)

    转载网址https://blog.csdn.net/Jason_Julie/article/details/82864187 1.按步骤安装Navicat Premium,如果没有可以去官网下载:ht ...

  6. ThinkPHP 3.2.2 事务

    手册里说得非常清楚 : 5.3.19 事务支持 ThinkPHP提供了单数据库的事务支持,如果要在应用逻辑中使用事务,可以参考下面的方法: 启动事务: PHP代码 $User->startTra ...

  7. ps 切片 蓝色 灰色 小标志 什么意思

    切片颜色 区分自动切片与用户切片和基于图层的切片.默认情况下,用户切片和基于图层的切片带蓝色标记,而自动切片带灰色标记.-----用户切片就是你需要的切片,其他区域会形成自动切片,可以视为自己不要的, ...

  8. vim打开多个文件方式及操作

    格式如下: #vim file*.txt 或者 #vim file file2 file3 查看当前编程的是那个文件,在冒号命令行下 :args 命令,类似:file [file2],以中括号里面为当 ...

  9. Elipse 快捷键

    1. eclipse里面如何快速收缩当前类文件里面的所有方法和注释收缩:ctrl+shift+/展开:ctrl+shift+*注意:这个/和*要是数字键盘上的/和*.   2. shift+enter ...

  10. O(1) 快速乘

    有一些毒瘤题,数据大小不光会炸\(int\),有时甚至会炸\(long long\).这时一个\(O(1)\)的防爆乘就很重要了 \(a*b%p\)可以转化为\(a*b-[a*b/p]*p\) 这里用 ...