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

Example:

Input:
[
["1","0","1","0","0"],
["1","0","1","1","1"],
["1","1","1","1","1"],
["1","0","0","1","0"]
]
Output: 6

此题是之前那道的 Largest Rectangle in Histogram 的扩展,这道题的二维矩阵每一层向上都可以看做一个直方图,输入矩阵有多少行,就可以形成多少个直方图,对每个直方图都调用 Largest Rectangle in Histogram 中的方法,就可以得到最大的矩形面积。那么这道题唯一要做的就是将每一层都当作直方图的底层,并向上构造整个直方图,由于题目限定了输入矩阵的字符只有 '0' 和 '1' 两种,所以处理起来也相对简单。方法是,对于每一个点,如果是 ‘0’,则赋0,如果是 ‘1’,就赋之前的 height 值加上1。具体参见代码如下:

解法一:

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

我们也可以在一个函数内完成,这样代码看起来更加简洁一些,注意这里的 height 初始化的大小为 n+1,为什么要多一个呢?这是因为我们只有在当前位置小于等于前一个位置的高度的时候,才会去计算矩形的面积,假如最后一个位置的高度是最高的,那么我们就没法去计算并更新结果 res 了,所以要在最后再加一个高度0,这样就一定可以计算前面的矩形面积了,这跟上面解法子函数中给 height 末尾加一个0是一样的效果,参见代码如下:

解法二:

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

下面这种方法的思路很巧妙,height 数组和上面一样,这里的 left 数组表示若当前位置是1且与其相连都是1的左边界的位置(若当前 height 是0,则当前 left 一定是0),right 数组表示若当前位置是1且与其相连都是1的右边界的位置再加1(加1是为了计算长度方便,直接减去左边界位置就是长度),初始化为n(若当前 height 是0,则当前 right 一定是n),那么对于任意一行的第j个位置,矩形为 (right[j] - left[j]) * height[j],我们举个例子来说明,比如给定矩阵为:

[
[1, 1, 0, 0, 1],
[0, 1, 0, 0, 1],
[0, 0, 1, 1, 1],
[0, 0, 1, 1, 1],
[0, 0, 0, 0, 1]
]

第0行:

h:
l:
r:

第1行:

h:
l:
r:

第2行:

h:
l:
r:

第3行:

h:
l:
r:

第4行:

h:
l:
r:

解法三:

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

再来看一种解法,这里我们统计每一行的连续1的个数,使用一个数组 h_max, 其中 h_max[i][j] 表示第i行,第j个位置水平方向连续1的个数,若 matrix[i][j] 为0,那对应的 h_max[i][j] 也一定为0。统计的过程跟建立累加和数组很类似,唯一不同的是遇到0了要将 h_max 置0。这个统计好了之后,只需要再次遍历每个位置,首先每个位置的 h_max 值都先用来更新结果 res,因为高度为1也可以看作是矩形,然后我们向上方遍历,上方 (i, j-1) 位置也会有 h_max 值,但是用二者之间的较小值才能构成矩形,用新的矩形面积来更新结果 res,这样一直向上遍历,直到遇到0,或者是越界的时候停止,这样就可以找出所有的矩形了,参见代码如下:

解法四:

class Solution {
public:
int maximalRectangle(vector<vector<char>>& matrix) {
if (matrix.empty() || matrix[].empty()) return ;
int res = , m = matrix.size(), n = matrix[].size();
vector<vector<int>> h_max(m, vector<int>(n));
for (int i = ; i < m; ++i) {
for (int j = ; j < n; ++j) {
if (matrix[i][j] == '') continue;
if (j > ) h_max[i][j] = h_max[i][j - ] + ;
else h_max[i][] = ;
}
}
for (int i = ; i < m; ++i) {
for (int j = ; j < n; ++j) {
if (h_max[i][j] == ) continue;
int mn = h_max[i][j];
res = max(res, mn);
for (int k = i - ; k >= && h_max[k][j] != ; --k) {
mn = min(mn, h_max[k][j]);
res = max(res, mn * (i - k + ));
}
}
}
return res;
}
};

类似题目:

Maximal Square

Largest Rectangle in Histogram

参考资料:

https://leetcode.com/problems/maximal-rectangle/

https://leetcode.com/problems/maximal-rectangle/discuss/29054/Share-my-DP-solution

https://leetcode.com/problems/maximal-rectangle/discuss/29172/My-O(n3)-solution-for-your-reference

https://leetcode.com/problems/maximal-rectangle/discuss/225690/Java-solution-with-explanations-in-Chinese

https://leetcode.com/problems/maximal-rectangle/discuss/29064/A-O(n2)-solution-based-on-Largest-Rectangle-in-Histogram

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Maximal Rectangle 最大矩形的更多相关文章

  1. leetcode Maximal Rectangle 单调栈

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052721.html 题目链接:leetcode Maximal Rectangle 单调栈 ...

  2. LeetCode: Maximal Rectangle 解题报告

    Maximal RectangleGiven a 2D binary matrix filled with 0's and 1's, find the largest rectangle contai ...

  3. [LintCode] Maximal Rectangle 最大矩形

    Given a 2D boolean matrix filled with False and True, find the largest rectangle containing all True ...

  4. [LeetCode] 85. Maximal Rectangle 最大矩形

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

  5. [LeetCode] Perfect Rectangle 完美矩形

    Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover o ...

  6. [leetcode]Maximal Rectangle @ Python

    原题地址:https://oj.leetcode.com/problems/maximal-rectangle/ 题意:Given a 2D binary matrix filled with 0's ...

  7. [LeetCode] 223. Rectangle Area 矩形面积

    Find the total area covered by two rectilinearrectangles in a 2D plane. Each rectangle is defined by ...

  8. leetcode -- Maximal Rectangle TODO O(N)

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...

  9. 085 Maximal Rectangle 最大矩形

    给定一个填充了 0 和 1 的二进制矩阵,找到最大的只包含 1 的矩形并返回其面积.例如,给出以下矩阵:1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0返回 6 详见:http ...

随机推荐

  1. SQLServer学习笔记系列1

    一.前言 一直自己没有学习做笔记的习惯,所以为了加强自己对知识的深入理解,决定将学习笔记写下来,希望向各位大牛们学习交流! 不当之处请斧正!在此感谢!这边就先从学习Sqlserver写起,自己本身对数 ...

  2. awk使用说明

    原文地址:http://www.cnblogs.com/verrion/p/awk_usage.html Awk使用说明 运维必须掌握的三剑客工具:grep(文件内容过滤器),sed(数据流处理器), ...

  3. Linux安装jdk

    查看Java的版本命令:java -version 查看java版本的方法是:运行--->cmd,输入java –version.注意: java命令后是有个空格的,-version表示参数而已 ...

  4. 记录一次bug解决过程:eclipse集成lombok插件

    一 总结 eclipse集成插件lombok: 启动Spring Boot项目: sublime全局搜索关键字:ctrl + shift + F JDK8中的lambda表达式使用 二 BUG描述:集 ...

  5. JAVA基础培训(isoft)

    我们

  6. html和html5详解

    最近看群里聊天聊得最火热的莫过于手机网站和html5这两个词.可能有人会问,这两者有什么关系呢?随着这移动互联网快速发展的时代,尤其是4G时代已经来临的时刻,加上微软对"XP系统" ...

  7. Canvas的width,height 和 样式中Canvas的width,height

    控制Canvas的大小,有两种方式: 1:直接设置Canvas标签上的书width,height属性值; 2:通过Css设置Canvas的width,height; 这两种方式,区别是很大的. 1:C ...

  8. Atitit ftp原理与解决方案

    Atitit ftp原理与解决方案 Deodeo sh shmayama ..search ftp.. 1. http和ftp都只是通信协议,就是只管传输那一块的,那为什么不能使用ftp来显示网页?? ...

  9. IOS 杂笔-14(被人遗忘的owner)

    *owner在开发中现在已经很少用了 有兴趣的童鞋可以看看* 我们遇到owner通常是在类似 [[[NSBundle mainBundle] loadNibNamed:@"Food" ...

  10. django ftp 研究

    做个网络运维多年,一直在用filezilla搭建ftp服务器,总体上还是不错的.但是用户配置与管理方面还是不太方便,希望能够通过web页面对用户进行管理和对ftp服务器进行监控. 工具: (1)dja ...