题目:

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. SpringMVC spring-servlet.xml配置

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  2. linux shell脚本: 自动监控网站状态并发送提醒邮件

    1.创建监控脚本:$ vi /alidata/shell/webcheck.sh #!/bin/sh weblist="/alidata/shell/weblist.txt" my ...

  3. [luogu3359]改造异或树

    [luogu3359]改造异或树 luogu 和之前某道题类似只有删边的话考虑倒着加边 但是怎么统计答案呢? 我们考虑以任意点为根dfs一遍求出每个点到根的路径异或和s[i] 这样任意两点x,y的路径 ...

  4. SocketAsyncEventArgs里的AcceptSocket能独立存在吗?

    独立存在是什么意思? 先来看一个例子.我们知道一个Socket对象(我们叫他ListenScoket)可以调用AcceptAsync并接受一个SocketAsyncEventArgs对象,如果操作成功 ...

  5. SUBMIT 用法

    [转自http://lz357502668.blog.163.com/blog/static/16496743201241195817597/] 1.最普通的用法 *Code used to exec ...

  6. Linux下自动清除MySQL日志文件

    MySQL运行过程中会生成大量的日志文件,占用不少空间,修改my.cnf文件配置bin-log过期时间,在Linux下自动清除MySQL日志文件 [mysqld] expire-logs-days= ...

  7. Linux中查找文件和文件内容的常用命令

    一.whereis <程序名称> 查找软件的安装路径-b 只查找二进制文件 -m 只查找帮助文件-s 只查找源代码-u 排除指定类型文件-f 只显示文件名-B <目录> 在指定 ...

  8. jQuery中通过JSONP来跨域获取数据的三种方式

    第一种方法是在ajax函数中设置dataType为'jsonp' $.ajax({ dataType: 'jsonp', url: 'http://www.a.com/user?id=123', su ...

  9. 第四章 python中的面向对象设计

    一.首先来理解几个面向对象的关键特性: 1.封装:对象可以将他们的内部状态隐藏起来.python中所有特性都是公开可用的. 2.继承:一个类可以是一个或多个类的子类.python支持多重继承,使用时需 ...

  10. Demo Nec

    /* 布局 */.g-va{width:1160px;margin:0 auto;} /* visual area */ /* 模块 */.m-nav{position:relative;height ...