leetcode面试准备: Maximal Rectangle
leetcode面试准备: Maximal Rectangle
1 题目
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.
接口: int maximalRectangle(char[][] matrix)
2 思路
这是一道非常综合的题目,要求在0-1矩阵中找出面积最大的全1矩阵。刚看到这道题会比较无从下手,brute force就是对于每个矩阵都看一下,总共有m(m+1)/2*n(n+1)/2个子矩阵(原理跟字符串子串类似,字符串的子串数有n(n+1)/2,只是这里是二维情形,所以是两个相乘),复杂度相当高,肯定不是面试官想要的答案,就不继续想下去了。
这道题的解法灵感来自于Largest Rectangle in Histogram这道题,假设我们把矩阵沿着某一行切下来,然后把切的行作为底面,将自底面往上的矩阵看成一个直方图(histogram)。直方图的中每个项的高度就是从底面行开始往上1的数量。根据Largest Rectangle in Histogram我们就可以求出当前行作为矩阵下边缘的一个最大矩阵。接下来如果对每一行都做一次Largest Rectangle in Histogram,从其中选出最大的矩阵,那么它就是整个矩阵中面积最大的子矩阵。
算法的基本思路已经出来了,剩下的就是一些节省时间空间的问题了。
我们如何计算某一行为底面时直方图的高度呢? 如果重新计算,那么每次需要的计算数量就是当前行数乘以列数。然而在这里我们会发现一些动态规划的踪迹,如果我们知道上一行直方图的高度,我们只需要看新加进来的行(底面)上对应的列元素是不是0,如果是,则高度是0,否则则是上一行直方图的高度加1。利用历史信息,我们就可以在线行时间内完成对高度的更新。我们知道,Largest Rectangle in Histogram的算法复杂度是O(n)。所以完成对一行为底边的矩阵求解复杂度是O(n+n)=O(n)。接下来对每一行都做一次,那么算法总时间复杂度是O(m*n)。
空间上,我们只需要保存上一行直方图的高度O(n),加上Largest Rectangle in Histogram中所使用的空间O(n),所以总空间复杂度还是O(n)
复杂度: Time: O(m*n); Space: O(n)
3 代码
public int maximalRectangle(char[][] matrix) {
int result = 0;
int row = matrix.length;
if (matrix == null || row == 0) {
return result;
}
int col = matrix[0].length; // O(n)空间
int[] height = new int[col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (matrix[i][j] == '0') {
height[j] = 0;
} else {
height[j] += 1;
}
}
result = Math.max(result, largestRectangleArea(height));
}
return result;
}
private int largestRectangleArea(int[] height) {
int result = 0;
int len = height.length;
Deque<Integer> stack = new LinkedList<Integer>();
for (int i = 0; i < len;) {
if (stack.isEmpty() || height[stack.peek()] < height[i]) {
stack.push(i);
i++;
} else {
int tmp = stack.pop();
int count = stack.isEmpty() ? i : i - stack.peek() - 1;
result = Math.max(result, count * height[tmp]);
}
}
while (!stack.isEmpty()) {
int tmp = stack.pop();
int count = stack.isEmpty() ? len : len - stack.peek() - 1;
result = Math.max(result, count * height[tmp]);
}
return result;
}
4 总结
O(m*n)时间内就可以完成对最大矩阵的搜索。动态规划、栈的思想。
5 参考
leetcode面试准备: Maximal Rectangle的更多相关文章
- 【LeetCode】85. Maximal Rectangle
Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...
- 【leetcode】85. Maximal Rectangle(单调栈)
Given a rows x cols binary matrix filled with 0's and 1's, find the largest rectangle containing onl ...
- 【LeetCode】85. Maximal Rectangle 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/maximal- ...
- 【一天一道LeetCode】#85. Maximal Rectangle
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- LeetCode OJ 85. Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and ...
- LeetCode OJ:Maximal Rectangle(最大矩形)
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- LeetCode解题报告—— Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and ...
- 【LeetCode】085. Maximal Rectangle
题目: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's ...
- LeetCode OJ 之 Maximal Square (最大的正方形)
题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...
随机推荐
- C语言:Day1~Day4
点击右键查看原图
- ES6的promise的学习
1.Promise的含义: Promise是异步编程的一种解决方案,比传统的解决方案——回调函数和事件——更合理和更强大.它由社区最早提出和实现,ES6将其写进了语言标准,统一了用法,原生提供了Pro ...
- 用VIM写作
Write in VIm 1.Writing in Vim by Dr. Bunsen
- VIM 拼写/spell check
VIM 拼写检查/spell check 一.Hunspell科普 Hunspell 作为一个拼写检查的工具,已经用在了许多开源的以及商业软件中.包括Google Chrome, Libreoffic ...
- 在Window IIS中安装运行node.js应用—你疯了吗
[原文发表地址]Installing and Running node.js applications within IIS on Windows - Are you mad? [原文发表时间]201 ...
- php 一维数组排序,保留key值
function sort_with_keyName($arr,$orderby='desc'){ //在内存的另一处 $a 复制内容与 $arr 一样的数组 foreach($arr as $key ...
- CentOS7 查看ip
查看内网:ip addr 查看公网:curl members.3322.org/dyndns/getip
- 支持HTML5新标签
IE8/IE7/IE6支持通过document.createElement方法产生的标签, 可以利用这一特性让这些浏览器支持HTML5新标签, ...
- Linux下GPIO驱动(五) ----misc_register();
// struct miscdevice { int minor; const char *name; const struct file_operations *fops; struct list_ ...
- Linux操作命令(一)
ls 命令 ls 命令是 linux 下最常用的命令,ls 命令就是 list 的缩写. ls 用来打印出当前目录的清单.如果 ls 指定其他目录,那么就会显示指定目录里的文件及文件夹清单. 通过 l ...