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 ...
随机推荐
- sqlldr errors
sqlldr myUser/myPWD@myCONN control='d:/sqlload/new/test/loader1.ctl' errors=1000000
- Spring 定时操作业务需求
1.定时分析 在业务需求中有的需要检测用户的状态,通过对用户状态的检测做出对此状态相应的操作,如果这种检测由运营人工检测,不仅工作量大,而且准确性不高,人工无法很好的完成工作: 问题根源:在检测用户状 ...
- python开发_tkinter_自己做的猜数字小程序
读到这篇文章[python 3.3下结合tkinter做的猜数字程序]的时候,就复制了代码,在自己机器上面跑了一下 源程序存在一个缺陷: 即当用户答对了以后,用户再点击'猜'按钮,最上面的提示标签还会 ...
- mysql反向解析导致连接缓慢
Content 0.序 1.问题 2.原因 3.解决办法 0.序 本文主要是记录Mysql安装在 VMWARE下,本地连接Mysql速度很慢的原因及解决办法. 1.问题 本地的一个网站使用mysql数 ...
- java中HashMap在多线程环境下引起CPU100%的问题解决
最近项目中出现了Tomcat占用CPU100%的情况,原以为是代码中出现死循环,后台使用jstack做了dump,发现是系统中不合理使用HashMap导致出现了死循环(注意不是死锁). 产生这个死循环 ...
- [JAVA] JAVA JDK 安装配置
JDK 安装 下载安装 下载JDK 从oracle官方网站下载并安装JDK. 下载使用文档 从oracle官方网站下载使用帮助文档. 安装库源文件 源文件位于安装目录的 /Library/Java/J ...
- .Net 中DataTable和 DataRow的 区别与联系
1.简要说明二者关系 DataRow 和 DataColumn 对象是 DataTable 的主要组件.使用 DataRow 对象及其属性和方法检索.评估.插入.删除和更新 DataTable 中的值 ...
- Ubuntu 11.10 (Oneiric)上编译带utrace补丁的内核 转
Ubuntu 11.10 (Oneiric)上编译带utrace补丁的内核 首先准备linux内核编译环境: sudo apt-get install fakeroot build-essential ...
- 苹果Mac OS X系统十三年视觉变化发展史
1Mac OS 9 一个普通的桌面操作系统 经过多个测试版本后,苹果终于正式公布OS X 10.10 Yosemite操作系统.苹果称这个第11版的OS X系统是自从2001年问世以来在视觉效果上变化 ...
- MyEclipse Web项目调试
1.发布项目 2.启动服务 服务有两种启动方式,Run Server和Debug Server Run Server是运行模式,Debug Server是调试模式 使用Debug Server模式启动 ...