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. javascript入门基础知识

    JavaScript介绍: 1. javascrip是互联网上最流行的脚本语言,可用于Web和HTML,更可广泛用于服务器.pc端.移动端. 2. javascript脚本语言: javascript ...

  2. Tiny6410之重定位代码到SDRAM

    在上一章中,将代码重定位到了SRAM中,但是这样的做法作用不大.正确的做法的是将代码重定位到更大的主存中,即DRAM.Tiny6410的DRAM控制寄存器最多只能支持两个同一类型的芯片.每个芯片最多可 ...

  3. FormsCookieName保存登录用户名的使用

    一,写一个类来实现 using System; using System.Collections.Generic; using System.Linq; using System.Web; using ...

  4. Java 序列化 JDK序列化总结

    Java 序列化 JDK序列化总结 @author ixenos Java序列化是在JDK 1.1中引入的,是Java内核的重要特性之一.Java序列化API允许我们将一个对象转换为流,并通过网络发送 ...

  5. 查询--游标 limit skip sort

    打印出所有的里程: var cursor = db.tblDaily.find(); cursor.forEach(function(x){ print(x.DailyCount + x.DailyU ...

  6. 建立tcl文件

  7. mac xcode 快捷键

    shift + cmd + n     新建项目 cmd + n             新建文件 视图 option + cmd + 回车 打开助理编辑器 cmd + 回车           显示 ...

  8. ubuntu 配置ftp服务器 vsftpd

    1. 更新库,否则会可能有库过时不匹配报错. sudo apt-get update 2. 安装vsftpd sudo apt-get install vsftpd 3. 判断vsftpd是否安装成功 ...

  9. 敏捷开发之Scrum

    现在敏捷开发是越来越火了,人人都在谈敏捷,人人都在学习Scrum和XP... 为了不落后他人,于是我也开始学习Scrum,今天主要是对我最近阅读的相关资料,根据自己的理解,用自己的话来讲述Scrum中 ...

  10. 防范CSRF(一)

    CSRF是跨网站伪造请求的缩写.大致的攻击流程是,黑客获得浏览器向服务器发送的请求,然后对请求进行修改,让服务器执行指定的操作. 防范方式可以使用微软提供的解决方案. View放置Html.AntiF ...