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

For example, given the following matrix:

1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0

Return 6.


【题目分析】

这个问题是要在一个0-1矩阵中找到由1构成的最大最大的矩阵的面积。


【思路】

如何下手呢?

我们把这个问题转换成之前已经解决过的问题,Largest Rectangle in Histogram。

对于每一行数据,如果该位置是1,则我们可以向上延伸找到该位置对应的一个height,处理完一行数据后可以得到一个高度行向量。例如题目中矩阵的

第二行对应的向量如下:

第三行对应的向量如下:

第四行对应的向量如下:

这样我们就把这个问题转换成了多个Largest Rectangle in Histogram问题,问题很容易就解决了。

【java代码】

 public class Solution {
public int maximalRectangle(char[][] matrix) {
int row = matrix.length;
if(row == 0) return 0;
int col = matrix[0].length;
if(col == 0) return 0; int maxArea = 0; for(int i = 0; i < row; i++){
int[] height = new int[col];
for(int j = 0; j < col; j++){
if(matrix[i][j] == '1'){
for(int k = i; k >=0; k--){
if(matrix[k][j] == '1'){
height[j]++;
}
else break;
}
}
}
maxArea = Math.max(maxArea, largestRectangleArea(height));
} return maxArea;
} public int largestRectangleArea(int[] height) {
int len = height.length;
Stack<Integer> s = new Stack<Integer>();
int maxArea = 0;
for(int i = 0; i <= len; i++){
int h = (i == len ? 0 : height[i]);
if(s.isEmpty() || h >= height[s.peek()]){
s.push(i);
}else{
int tp = s.pop();
maxArea = Math.max(maxArea, height[tp] * (s.isEmpty() ? i : i - 1 - s.peek()));
i--;
}
}
return maxArea;
}
}

LeetCode OJ 85. 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:Maximal Rectangle(最大矩形)

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

  6. [LeedCode OJ]#85 Maximal Rectangle

     [ 声明:版权全部,转载请标明出处.请勿用于商业用途. 联系信箱:libin493073668@sina.com] 题目链接:https://leetcode.com/problems/maxima ...

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

  8. 85. Maximal Rectangle

    85. Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle c ...

  9. leetcode面试准备: Maximal Rectangle

    leetcode面试准备: Maximal Rectangle 1 题目 Given a 2D binary matrix filled with 0's and 1's, find the larg ...

随机推荐

  1. [转载]We Recommend a Singular Value Decomposition

    原文:http://www.ams.org/samplings/feature-column/fcarc-svd Introduction The topic of this article, the ...

  2. [转]numpy中的matrix矩阵处理

    今天看文档发现numpy并不推荐使用matrix类型.主要是因为array才是numpy的标准类型,并且基本上各种函数都有队array类型的处理,而matrix只是一部分支持而已. 这个转载还是先放着 ...

  3. BOGEER博格尔YT-813码表使用说明书 (我的是YT-823)

    BOGEER博格尔YT-813码表使用说明书.doc 源:http://w.gdu.me/wiki/Bike/BOGEER-YT-813.html 参数设置 首先要测量出车轮的周长,测出车轮周长后按住 ...

  4. Python网络编程学习_Day11

    一.协程 1.理论知识 协程,又称伪线程,是一种用户态的轻量级线程. 协程拥有自己的寄存器上下文和栈,协程调度切换时,将寄存器上下文和栈保存到其他地方,在切回来的时候,恢复先前保存的寄存器上下文和栈. ...

  5. get请求与post请求之间的差异

    GET:常用于向服务器请求查询某些信息 get请求适用于当URL完全指定请求资源. get请求不会对数据库进行任何操作相当于数据库的查询.  当进行字段查询时可将查询字段增加到url的末尾. get请 ...

  6. js 数值格式化函数

    function ForDight(Dight,How){ ,How))/Math.pow(,How); return Dight; } //ForDight(Dight,How):数值格式化函数; ...

  7. css学习之 display:inline-block;

    设置display:inline-block;后的元素 就是一个格式化为行内元素的块容器( Block container ):通俗讲就是:将对象呈递为内联对象,但是对象的内容作为块对象呈递.旁边的内 ...

  8. CodeForces 667C Reberland Linguistics

    $dp$. 题意中有一个词组:$in$ $a$ $row$,是连续的意思.... 因此这题只要倒着$dp$一下就可以了.$f[i][0]$表示从$i$位置往后割两个能否割,$f[i][1]$表示从$i ...

  9. java 打开浏览器 url

    public class openBrowers { public static void main(String[] args) { try { //String url = "http: ...

  10. CentOS 7 systemd service开机启动设定

    #vi /etc/systemd/system/xxx.service [Unit] Description=startup script test [Service] Type=simple Exe ...