Maximal Rectangle leetcode java
题目:
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.
题解:
这道题可以应用之前解过的Largetst Rectangle in Histogram一题辅助解决。解决方法是:
按照每一行计算列中有1的个数,作为高度,当遇见0时,这一列高度就为0。然后对每一行计算 Largetst Rectangle in Histogram,最后得到的就是结果。
代码如下:
1 public int maximalRectangle(char[][] matrix) {
2 if(matrix==null || matrix.length==0 || matrix[0].length==0)
3 return 0;
4 int m = matrix.length;
5 int n = matrix[0].length;
6 int max = 0;
7 int[] height = new int[n];//对每一列构造数组
8 for(int i=0;i<m;i++){
9 for(int j=0;j<n;j++){
if(matrix[i][j] == '0')//如果遇见0,这一列的高度就为0了
height[j] = 0;
else
height[j] += 1;
}
max = Math.max(largestRectangleArea(height),max);
}
return max;
}
public int largestRectangleArea(int[] height) {
Stack<Integer> stack = new Stack<Integer>();
int i = 0;
int maxArea = 0;
int[] h = new int[height.length + 1];
h = Arrays.copyOf(height, height.length + 1);
while(i < h.length){
if(stack.isEmpty() || h[stack.peek()] <= h[i]){
stack.push(i);
i++;
}else {
int t = stack.pop();
int square = -1;
if(stack.isEmpty())
square = h[t]*i;
else{
int x = i-stack.peek()-1;
square = h[t]*x;
}
maxArea = Math.max(maxArea, square);
}
}
return maxArea;
}
Maximal Rectangle leetcode java的更多相关文章
- Maximal Rectangle [leetcode] 的三种思路
第一种方法是利用DP.时间复杂度是 O(m * m * n) dp(i,j):矩阵中同一行以(i,j)结尾的所有为1的最长子串长度 代码例如以下: int maximalRectangle(vecto ...
- Maximal Rectangle [LeetCode]
Problem Description: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle co ...
- LeetCode: Maximal Rectangle 解题报告
Maximal RectangleGiven a 2D binary matrix filled with 0's and 1's, find the largest rectangle contai ...
- 求解最大矩形面积 — leetcode 85. Maximal Rectangle
之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...
- leetcode Maximal Rectangle 单调栈
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052721.html 题目链接:leetcode Maximal Rectangle 单调栈 ...
- leetcode面试准备: Maximal Rectangle
leetcode面试准备: Maximal Rectangle 1 题目 Given a 2D binary matrix filled with 0's and 1's, find the larg ...
- [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- 【leetcode】Maximal Rectangle
Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...
- 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 ...
随机推荐
- 59. 螺旋矩阵 II
给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, ...
- 2018年全国多校算法寒假训练营练习比赛(第一场)J - 闯关的lulu
链接:https://www.nowcoder.com/acm/contest/67/J来源:牛客网 题目描述 勇者lulu某天进入了一个高度10,000,000层的闯关塔,在塔里每到一层楼,他都会获 ...
- HDU - 2199 Can you solve this equation? 二分 简单题
Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- Javascript:window.close()不起作用?
一般的窗口关闭的JS如下写法: window.close() 但是呢,chrome,firefox等中有时候会不起作用. 改为下面的写法: window.open("about:blank& ...
- POJ 3177 Redundant Paths 双联通分量 割边
http://poj.org/problem?id=3177 这个妹妹我大概也曾见过的~~~我似乎还没写过双联通分量的blog,真是智障. 最少需要添多少条边才能使这个图没有割边. 边双缩点后图变成一 ...
- 【BZOJ】4985: 评分【DP】
4985: 评分 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 148 Solved: 75[Submit][Status][Discuss] Des ...
- 【openjudge】 CDQZ challenge 4
改了三天,提交17次,一定要纪念一下! 1004:Challenge 4 总时间限制: 10000ms 单个测试点时间限制: 1000ms 内存限制: 262144kB 描述 给一个长为N的数列 ...
- Python环境右键定制
有时候,我们需要将py打包成exe.需要将ui转换成py.需要将py转换成pyc等等,命令行操作起来有点繁琐.所以做了这个教程: 1. py打包成exe 先安装cx_freeze,参照教程:http: ...
- Codeforces Round #353 (Div. 2) A. Infinite Sequence 水题
A. Infinite Sequence 题目连接: http://www.codeforces.com/contest/675/problem/A Description Vasya likes e ...
- Linux性能监控分析命令(二)—sar命令介绍
性能监控分析的命令包括如下: 1.vmstat 2.sar 3.iostat 4.top 5.free 6.uptime 7.netstat 8.ps 9.strace 10.lsof ======= ...