085 Maximal Rectangle 最大矩形
给定一个填充了 0 和 1 的二进制矩阵,找到最大的只包含 1 的矩形并返回其面积。
例如,给出以下矩阵:
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
返回 6
详见:https://leetcode.com/problems/maximal-rectangle/description/
Java实现:
class Solution {
public int maximalRectangle(char[][] matrix) {
if(matrix == null || matrix.length == 0 || matrix[0].length == 0){
return 0;
}
int res = 0;
int m = matrix.length;
int n = matrix[0].length;
int [] heights = new int[n];
for(int i = 0; i<m; i++){
for(int j = 0; j<n; j++){
heights[j] = matrix[i][j] == '0' ? 0 : heights[j]+1;
}
res = Math.max(res, largestRectangleArea(heights));
}
return res;
}
private int largestRectangleArea(int[] heights) {
int res=0;
int n=heights.length;
for(int i=0;i<n;++i){
if(i+1<n&&heights[i]<=heights[i+1]){
continue;
}
int minH=heights[i];
for(int j=i;j>=0;--j){
minH=Math.min(minH,heights[j]);
int area=minH*(i-j+1);
res=Math.max(res,area);
}
}
return res;
}
}
参考:https://www.cnblogs.com/grandyang/p/4322667.html
085 Maximal Rectangle 最大矩形的更多相关文章
- [LintCode] Maximal Rectangle 最大矩形
Given a 2D boolean matrix filled with False and True, find the largest rectangle containing all True ...
- [LeetCode] Maximal Rectangle 最大矩形
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- [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】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 085 Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- 求解最大矩形面积 — leetcode 85. Maximal Rectangle
之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...
- 最大的矩形面积 Maximal Rectangle
2018-09-15 10:23:44 一.Largest Rectangle in Histogram 在求解最大的矩形面积之前,我们先讨论一条最大直方图面积的问题. 问题描述: 问题求解: 解法一 ...
- LeetCode 84--柱状图中最大的矩形( Largest Rectangle in Histogram) 85--最大矩形(Maximal Rectangle)
84题和85五题 基本是一样的,先说84题 84--柱状图中最大的矩形( Largest Rectangle in Histogram) 思路很简单,通过循环,分别判断第 i 个柱子能够延展的长度le ...
- 【leetcode】Maximal Rectangle
Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...
随机推荐
- Codeforces Round #369 (Div. 2) D. Directed Roads —— DFS找环 + 快速幂
题目链接:http://codeforces.com/problemset/problem/711/D D. Directed Roads time limit per test 2 seconds ...
- hadoop学习之旅2
集群搭建文档1.0版本 1. 集群规划 所有需要用到的软件: 链接:http://pan.baidu.com/s/1jIlAz2Y 密码:kyxl 2.0 系统安装 2.1 主机名配置 vi /etc ...
- Yii2.0 自动生成 model 层
yii2.0 里一个表对应一个model,可以自动生成 前台使用的model在frontend(backend)/web目录下的gii生成例如(www.liqiuyue.com/yii /fronte ...
- hdu-5621 KK's Point(dp+数学)
题目链接: KK's Point Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- Python:条件判断
条件控制:if...elif...else if condition_1: statement_1 elif condition_2: statement_2 else: statement_3 判断 ...
- Messes in Reading Source Coding of SSD
这里记录在学习SSD源码过程中用到的相关内容 keras.applications.imagenet_utils.preprocess_input(): 用来将读入的原始图片张量转换成为需要Image ...
- mysql审计实现方法
Mysql版本: 5.6.24-72.2 一.通过init-connect + binlog 实现MySQL审计功能 基本原理: 由于审计的关键在于DML语句,而所有的DML语句都可以通过binlog ...
- PHP mysql_fetch_array与mysql_fetch_row的区别
如果你的表里面有字段a,b,c那么你用mysql_fetch_row() 就返回array(1=>a的值,2=>b的值,3=>c的值)这个时候你读数组的话,只能这样写$array[1 ...
- C#视频取帧图
由于项目里页面有许多视频资料需要展示给用户查看,因此需要做一个视频列表,原设计是列表显示视频第一帧图,但实际上很多视频第一帧是纯黑底色. 于是想到用js利用canvas截图,最后发现由于浏览器跨域限制 ...
- hdu-1597
find the nth digit Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...