Java for LeetCode 085 Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.
解题思路:
求01矩阵中,全是1的子矩阵的最大面积。
把矩阵按照每一行看做是直方图,可以转化为上一题,JAVA实现如下:
static public int maximalRectangle(char[][] matrix) {
if(matrix.length==0||matrix[0].length==0)
return 0;
int result=0;
int[] dp=new int[matrix[0].length];
for(int i=0;i<matrix.length;i++){
for(int j=0;j<matrix[0].length;j++)
if(matrix[i][j]=='1')
dp[j]++;
else dp[j]=0;
result=Math.max(result, largestRectangleArea(dp));
}
return result;
}
public static int largestRectangleArea(int[] height) {
Stack<Integer> stk = new Stack<Integer>();
int ret = 0;
for (int i = 0; i <= height.length; i++) {
int h=0;
if(i<height.length)
h=height[i];
if (stk.isEmpty() || h >= height[stk.peek()])
stk.push(i);
else {
int top = stk.pop();
ret = Math.max(ret, height[top] * (stk.empty() ? i : i - stk.peek() - 1));
i--;
}
}
return ret;
}
Java for LeetCode 085 Maximal Rectangle的更多相关文章
- 求解最大矩形面积 — leetcode 85. Maximal Rectangle
之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...
- 【leetcode】Maximal Rectangle
Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...
- 【LeetCode】085. Maximal Rectangle
题目: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's ...
- Java for LeetCode 084 Largest Rectangle in Histogram【HARD】
For example, Given height = [2,1,5,6,2,3], return 10. 解题思路: 参考Problem H: Largest Rectangle in a Hist ...
- [LeetCode] 85. Maximal Rectangle 最大矩形
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and ...
- LeetCode (85): Maximal Rectangle [含84题分析]
链接: https://leetcode.com/problems/maximal-rectangle/ [描述] Given a 2D binary matrix filled with '0's ...
- 085 Maximal Rectangle 最大矩形
给定一个填充了 0 和 1 的二进制矩阵,找到最大的只包含 1 的矩形并返回其面积.例如,给出以下矩阵:1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0返回 6 详见:http ...
- Java for LeetCode 221 Maximal Square
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...
- 【leetcode】Maximal Rectangle (hard)★
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
随机推荐
- linux调整缓存写入磁盘的时间,减少磁盘爆掉的可能性
缓存数据存入磁盘的最长时间,如果这段时间写不完,就会报异常停止写,这样缓存数据会不断积累,导致内存爆掉. echo 0 > /proc/sys/kernel/hung_task_timeout_ ...
- 查找python项目依赖并生成requirements.txt
1.如果使用virtualenv环境,直接使用 pip freeze > requirements.txt ➜ ~ .virtualenvs/xxx/bin/pip freeze > r ...
- VS2010 MFC中 给菜单项添加消息响应函数
久了没用,居然忘记了该怎样给菜单项添加响应函数了~~~~~~~~T_T 特记于此: 1. 在资源视图的Menu里找到自己要添加的菜单,然后输入菜单项. 2. 右击菜单项选属性,设置Popup为Fals ...
- 23. Spring Boot启动加载数据CommandLineRunner【从零开始学Spring Boot】
转:http://blog.csdn.net/linxingliang/article/details/52069503 实际应用中,我们会有在项目服务启动的时候就去加载一些数据或做一些事情这样的需求 ...
- 【Python】写入文件
1.1写入空文件 若将文本写入文件,在调用open()时候需要提供另外一个实参,告诉Python你要写入打开的文件 file_path = 'txt\MyFavoriteFruit.txt' with ...
- 分享下多年积累的对JAVA程序员成长之路的总结
http://blog.csdn.net/zhongzelin/article/details/8643269我也搞了几年JAVA了,由于一向懒惰,没有成为大牛,只是一普通程序猿,不爱玩社交网站,不爱 ...
- python selenium2 - 鼠标键盘操作
文件路径:Python27\Lib\site-packages\selenium\webdriver\common\action_chains.py action_chains[鼠标键盘动作] 方法说 ...
- Android后退事件的处理
当我们想退出应用程序时,一般都会采用按物理按键(后退键)的做法,当用户在按两次后退键的时候就将应用程序退出,即销毁当前的Activity(): 重写onBackPressed()方法即可: 代码如下: ...
- 33:字符统计SumOfCharactors
题目描述:如果统计的个数相同,则按照ASII码由小到大排序输出 .如果有其他字符,则对这些字符不用进行统计. 实现以下接口: 输入一个字符串,对字符中的各个英文字符,数字,空格进行统计(可反复调用) ...
- 在 Ubuntu16.04上安装并使用Docker
介绍 Docker是一个开放源代码软件项目,让应用程序布署在软件容器下的工作可以自动化进行,借此在Linux操作系统上,提供一个额外的软件抽象层,以及操作系统层虚拟化的自动管理机制[1].Docker ...