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. 20140717 随笔 技术文章安家至CSDN

    今天意外的看到了阳光小强的博客,接着在CSDN里看着各种博客,又有一种冲动,想在CSDN里安家.我是一个不安分的人,或者说是不能坚持的人,喜新厌旧属于我吧.看到新鲜的就要去试一下,但是能够坚持下来的却 ...

  2. leetcode之Count Complete Tree Nodes

    Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...

  3. CentOS7 查看ip

    查看内网:ip addr 查看公网:curl members.3322.org/dyndns/getip

  4. mysql 之权限介绍

    转自:http://tech.it168.com/a2010/0114/837/000000837456_all.shtml 一.MySQL授权表概述首先从全局开始,如果全局的是允许的,即在 user ...

  5. CSS实现文字上标、下标

    .sup{vertical-align:super; color:red; font-size:9px; font-family:Arial, Helvetica, sans-serif; margi ...

  6. 13个Cat命令管理(显示,排序,建立)文件实例

    在Linux系统中,大多数配置文件.日志文件,甚至shell脚本都使用文本文件格式,因此,Linux系统存在着多种文本编辑器,但当你仅仅想要查看一下这些文件的内容时,可使用一个简单的命令-cat. c ...

  7. Django操作数据库

    引入models的定义 from app.models import  myclass class  myclass():      aa =  models. CharField (max_leng ...

  8. office 问题集

    ----20131012---------------------------------------------------------------------------------------- ...

  9. jquery 插件页面回到顶部

    引用: jquery.scrollUp.min.js js: $.scrollUp({ scrollName: 'scrollUp', // Element ID topDistance: '300' ...

  10. sqlserver分页;mysql分页;orcale分页 的sql 查询语句

    一,sqlserver分页: )) ID FROM 表名 AS 表名1_1 ORDER BY ID)) ORDER BY ID; 二:mysql分页 )*每页显示的数据条数,当前页码 * 每页显示的数 ...