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 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的更多相关文章
- 【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:Maximal Rectangle(最大矩形)
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- [LeedCode OJ]#85 Maximal Rectangle
[ 声明:版权全部,转载请标明出处.请勿用于商业用途. 联系信箱:libin493073668@sina.com] 题目链接:https://leetcode.com/problems/maxima ...
- 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 ...
- 85. Maximal Rectangle
85. Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle c ...
- leetcode面试准备: Maximal Rectangle
leetcode面试准备: Maximal Rectangle 1 题目 Given a 2D binary matrix filled with 0's and 1's, find the larg ...
随机推荐
- [Python]-类型转换
1.字符串到数值的转换:int(s [,base ]) 将表达式s转换为一个整数 ,s可以是整数,与数字有关的字符串,布尔类型long(s [,base ]) 将表达式s转换为一个长整数 s可以是整数 ...
- js函数的可变参数
//对于js的可变参数的清空,在定义函数式不需要写上参数, 在函数内部使用argument对象可以 直接获取参数个数等信息 //在调用函数式可以传递任意个数的参数 function text(){ v ...
- MVC 之下载 我的实践
Controller 1. public ActionResult DownLoad(string path,string fileName) { return File(new FileStream ...
- 图片添加border 不占用图片的大小
因为设计稿中的分割线大多分为两种情况:1.在图片右侧:2.在图片右侧+下方. 那么使用伪类before和after以及绝对定位很容易在不改变原布局的情况使图片按照设计稿输出和保留分割线. 例div.i ...
- MySql 安装过程(摘自网络)
下面的是MySQL安装的图解,用的可执行文件安装的,详细说明了一下! 打开下载的mysql安装文件mysql-5.0.27-win32.zip,双击解压缩, 运行“setup.exe”,出现如下界面 ...
- Java开发工具
总的来看Java的开发工具无非由两个部分构成,一个JDK一个是IDE,无论JDK是不可变的,IDE则是哪个用着舒服就用哪个. 1JDK 1.1简介 JDK是 Java 语言的软件开发工具包(SDK), ...
- 没事抽空学——c语言指针操作基础概念
指针基础 理解指针的最佳方法是画图,学习使用基本指针,不要产生空指针. 存储控件分配 存储控件分配是指在内存预留空间的过程.就像一个虚拟菜谱一样,指针对应菜名,其所指的内存空间中的数据对应实际的菜. ...
- Network: Why 1472B length of ICMP?
when ping, specifying the length of the packet by: ping localhost -l 32 Actually default is -l 32, s ...
- Installation LEK Cluster
The structure of cluster like this: We used four VM: A 10.32.xxx.213 ubuntu12.04 B 10.32.xxx.214 ...
- mysql for windows zip版安装
1.将mysql_5.6.24_winx64.zip 解压到文件夹 2.增加环境变量 3.修改mysql配置文件 将mysql根目录下的my-default.ini 复制一份更名为 my.ini.修改 ...