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 ...
随机推荐
- Concept with HTTP API && RPC
RPC=Remote Produce Call 是一种技术的概念名词. HTTP是一种协议,RPC可以通过HTTP来实现,也可以通过Socket自己实现一套协议来实现.所以楼主可以换一个问法,为何RP ...
- IB_DESIGNABLE 和 IBInspectable 的用法
我们经常会在用一些自定义 UIView 来完成一些特殊的UI效果,但是怎么让我自定义的 UIView 在 Storyboard 中预览和修改一些自定义参数呢.这就需要用到两个吊吊的东西. IB_DES ...
- HUST——1110雪碧(简单DFS)
1110: 雪碧 时间限制: 1 Sec 内存限制: 128 MB 提交: 18 解决: 6 题目描述 杨神最近特别喜雪碧,他现在有两瓶,他大晚上的在街上走,他逢店加一倍(雪碧),逢摊吃大虾并喝一 ...
- BZOJ3534 [Sdoi2014]重建 【矩阵树定理】
题目 T国有N个城市,用若干双向道路连接.一对城市之间至多存在一条道路. 在一次洪水之后,一些道路受损无法通行.虽然已经有人开始调查道路的损毁情况,但直到现在几乎没有消息传回. 辛运的是,此前T国政府 ...
- formData使用总结
1.formData基本使用 //可以从form元素初始化一个FormData对象,或者new一个空对象 var formData = new FormData([fromElement]); //可 ...
- ActiveMQ使用经验与优化
摘自:http://blog.csdn.net/m13321169565/article/details/8081314 1.1 不要频繁的建立和关闭连接 JMS使用长连接方式,一个程序,只要和JMS ...
- P1875 佳佳的魔法药水 (最短路,DP)
题目链接 Solution 好题. 一开始一直在想怎么蛇皮建图,但是发现一直蛇不出来... 正解是用类似于 dijkstra 的算法对所有点进行松弛. 对于每个元素记录两个值: \(cost\) 代表 ...
- [暑假集训--数位dp]hdu5898 odd-even number
For a number,if the length of continuous odd digits is even and the length of continuous even digits ...
- 关于pymongo的一些说明
问题 一: 在pymongo中使用find是得到1个游标对象的,如果你想实现MongoDB shell中find操作,例如: > db.test.find() { "_id" ...
- es6模板语法使用上的一点问题
var str = "test"; console.log(str); // test console.log(`str`) //str 这里的str是模板语法里面的,而不是变量s ...