LeetCode85 Maximal Rectangle java题解
public static int maximalRectangle(char[][] matrix) {
int rowNum=matrix.length;
if(rowNum==0)
return 0;
int columnNum=matrix[0].length;
int[][] height=new int[rowNum][columnNum+1];
int maxarea=0;
for(int i=0;i<rowNum;i++)
{
for(int j=0;j<columnNum;j++)
{
int k=i;
height[i][j]=0;
while(k>=0&&j<columnNum)
{
if(matrix[k][j]=='1')
height[i][j]++;
else
break;
k--;
}
}
height[i][columnNum]=-1;
}
Stack<Integer> stack=new Stack<>();
for(int i=0;i<rowNum;i++)
{
for(int j=0;j<=columnNum;j++)
{
int a=height[i][j];
int b=stack.isEmpty()?-1:stack.peek();
if(stack.isEmpty()||height[i][j]>=height[i][stack.peek()])
stack.push(j);
else
{
int tempPop=stack.pop();
maxarea=Math.max(maxarea, height[i][tempPop]*(stack.isEmpty()?
j:j-1-stack.peek()));
j--;
}
}
stack.clear();
}
return maxarea;
}
题目:
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.
也就是说,给一个仅仅有0和1的2维矩阵,求当中1可以组成的最大四方形面积
解题:
这题能够看作是前面一题(LeetCode84)的拓展,前面一题中输入的是一个数组,数组每个元素的值看作的矩形的高度,在这题中先对矩阵做一个处理。对矩阵的元素计算其高度,处理完之后得到一个每个原矩阵元素的高度矩阵,把这个矩阵当作输入就和前面一题是类似了。
代码:
LeetCode85 Maximal Rectangle java题解的更多相关文章
- 85. Maximal Rectangle (JAVA)
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- leetcode85 Maximal Rectangle
思路: 分别按行拆分后将这一行之前的每列叠加起来,然后使用leetcode84https://www.cnblogs.com/wangyiming/p/9620942.html的思路计算. 实现: # ...
- 求解最大矩形面积 — leetcode 85. Maximal Rectangle
之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...
- LeetCode之“动态规划”:Maximal Square && Largest Rectangle in Histogram && Maximal Rectangle
1. Maximal Square 题目链接 题目要求: Given a 2D binary matrix filled with 0's and 1's, find the largest squa ...
- LeetCode: Maximal Rectangle 解题报告
Maximal RectangleGiven a 2D binary matrix filled with 0's and 1's, find the largest rectangle contai ...
- 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
Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...
- [LintCode] Maximal Rectangle 最大矩形
Given a 2D boolean matrix filled with False and True, find the largest rectangle containing all True ...
- 47. Largest Rectangle in Histogram && Maximal Rectangle
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
随机推荐
- 用SVGDeveloper制作svg地图
项目中需要实现巴蜀地区图,并且将其分为川东.川西.川南.川北四个区域,鼠标悬浮对应区域的区块改变颜色.经过网上查询资料,并未找到现成的区域图,于是就利用SVGDeveloper工具绘制. 一.绘制地图 ...
- Java面试题之有没有有顺序的Map实现类,如果有,他们是怎么实现有序的?
Hashmap和Hashtable 都不是有序的. TreeMap和LinkedHashmap都是有序的.(TreeMap默认是key升序,LinkedHashmap默认是数据插入顺序) TreeMa ...
- Nginx+keepalived构建双主负载均衡代理服务器
引言 Nginx是一个高性能的代理服务器,单台Nginx容易出现单点故障,使用keepalived可以实现Nginx的故障转移,保证了网站的高可用性 一.使用Nginx+keepalived的两种方案 ...
- 马士兵hadoop第五课:java开发Map/Reduce(转)
马士兵hadoop第一课:虚拟机搭建和安装hadoop及启动 马士兵hadoop第二课:hdfs集群集中管理和hadoop文件操作 马士兵hadoop第三课:java开发hdfs 马士兵hadoop第 ...
- css3 背景图动画一
一 实现背景图循环播放 @keyframes mlfly { 0%{ background-position:0 0; } 100%{ background-position:210px 0; } } ...
- Java基础加强-(注解,动态代理,类加载器,servlet3.0新特性)
1. Annotation注解 1.1. Annotation概述 Annotation是JDK 5.0以后提供对元数据的支持,可以在编译.加载和运行时被读取,并执行相应的处理.所谓Annota ...
- yii 数据save后得到插入id
$model->save();//得到上次插入的Insert id$id = $model->attributes['id'];如此很简单
- LeetCode OJ——Validate Binary Search Tree
http://oj.leetcode.com/problems/validate-binary-search-tree/ 判断一棵树是否为二叉搜索树.key 是,在左子树往下搜索的时候,要判断是不是子 ...
- html5(拖拽3)
<!DOCTYPE html"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> ...
- Java 基础【05】你的多继承纳?
Java省略了许多很少用到,缺乏了解,混淆功能的C + +,在我们的经验中带来更多的悲伤大于收益 . -----James Gosling James Gosling 这个人大家应该很熟悉,就是最初设 ...