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 ...
随机推荐
- 如何防止SWF文件被反编译
这篇文章的标题所提出的问题的答案是“不可能”.至少对我来说是不可能的.借助适当的工具,我们可以反编译任何SWF文件.所以,不要将重要的信息置于SWF文件中.SWF文件中不要包含个人的帐号或者密码. 我 ...
- IIS7 发现无法显示ewebeditor编辑器成空白
vs2003写的网站,很早了,编辑器用的是ewebeditor,每次更换程序编辑器都会出问题.今天记录一下. 内部老网站在Windows2003 iis6上运行的. 现在要迁移到2008上64位.08 ...
- C++中map用法
/************************************************************************** Map的特点: 1.存储Key-value对* ...
- HowTo: SVN undo add without reverting local changes
Reference: http://stackoverflow.com/questions/5083242/undo-svn-add-without-reverting-local-edits svn ...
- Java小程序---接口中抽象方法的实现(解决了JAVA语言不能多继承的问题)
public interface Sing { public static final String eyecolor="black"; public void sleep(); ...
- 用LinqToExcel处理有标题表格的数据
1. 先根据表格标题定义一个类. public class News { public string Title { set; get; } public string Content { set; ...
- 关于静态库和动态库的理解(C++)
库的存在,是软件模块化的基础. 库存在的意义: } 库是别人写好的现有的,成熟的,可以复用的代码,你可以使用但要记得遵守许可协议. } 现实中每个程序都要依赖很多基础的底层库,不可能每个人的 ...
- shopnc 商城源码阅读笔记-缓存技术
缓存方式 : 从 shopnc 的缓存驱动目录 /framework/cache里已有的实现类来看,shopnc支持以下5种缓存方式 apc Eaccelerator file memcache xc ...
- Linux下GPIO驱动(一) ----一个简单的LED驱动
/******************************* * *杂项设备驱动:miscdevice *majior=10; * * *****************************/ ...
- poj 1904 King's Quest
King's Quest 题意:有N个王子和N个妹子;(1 <= N <= 2000)第i个王子喜欢Ki个妹子:(详见sample)题给一个完美匹配,即每一个王子和喜欢的一个妹子结婚:问每 ...