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的更多相关文章

  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 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 ...

  6. LeetCode OJ:Maximal Rectangle(最大矩形)

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

  7. LeetCode解题报告—— Maximal Rectangle

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

  8. 【LeetCode】085. Maximal Rectangle

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

  9. 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 ...

随机推荐

  1. javascript Date类型 学习笔记

    1 创建一个新的日期对象,如果不带参数,则对象自动获得当前的日期和时间 var d = new Date() 2 如果需要指定特定的日期,则可以通过Date.parse() 或者 Date().UTC ...

  2. Codevs 1535 封锁阳光大学

    1535 封锁阳光大学 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题目描述 Description 曹是一只爱刷街的老曹,暑假期间,他每天都欢快地在阳光大 ...

  3. Word 2010巧妙绘制各种分割线的方法(图文)

    引用: 使用Word编辑文档时,可能为了使某些内容醒目显示,或者为了使文档内容显示的更美观.更有层次感,需要为文档添加一些分割线,如添加下框线.插入水平线.使用特殊符号快速绘制分割线等等.在Word ...

  4. ubuntu12.10可用更新源

    ubutnu12.10自带的更新源已经失效,国内各大服务器的更新源,无论是网易.搜狐还是教育网的因此也失效了.附件是ubuntu12.10目前的可用更新源. 将地址中的“us.archive”换成“o ...

  5. 在Mac OS X中使用VIM开发STM32(3)

    本文原创于http://www.cnblogs.com/humaoxiao,非法转载者请自重!       在上一篇文章中,我们安装了ctags插件,ctags能对我们的源代码文件中的元素建立索引表, ...

  6. linux 终端快捷键

    1. 移动光标快捷键 ctrl+f 向前移动一个字符 ctrl+b 向后移动一个字符 alt+f 向前移动一个单词 alt+b 向后移动一个单词 ctrl+a 移动到当前行首 ctrl+e 移动到当前 ...

  7. Oracle if else if for case

    ------------------游标+for+if else if DECLARE cursor s_cursor is SELECT * from emp;--定义游标 begin for r ...

  8. opensuse安装mysql-workbench

    首先安装好mysqlserver,做本地测试其实不用安装mysqlclient. 到http://software.opensuse.org/package/mysql-workbench 下载相应版 ...

  9. ASP.NET MVC验证 - jQuery异步验证

    本文主要体验通过jQuery异步验证. 在很多的教材和案例中,MVC验证都是通过提交表单进行的.通过提交表单,可以很容易获得验证出错信息.因为,无论是客户端验证还是服务端验证,总能找到与Model属性 ...

  10. css写圆角效果

    .introTips i{ position: absolute; display: block; top: 8px; right: 8px; width:; height:; font-size:; ...