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 ...
随机推荐
- mysql批量sql插入优化
对于一些数据量较大的系统,数据库面临的问题除了查询效率低下,还有就是数据入库时间长.特别像报表系统,每天花费在数据导入上的时间可能会长达几个小时或十几个小时之久.因此,优化数据库插入性能是很有意义的. ...
- linux应用之gcc编译器的安装及使用
gcc是linux系统下功能十分强大的编译器. 本人使用的是CentOS 6.6 64位系统,由于在安装系统的时候并没有勾选安装gcc编译器,因此需要自行安装gcc编译器. 使用yum安装gcc 对于 ...
- BZOJ_2989_数列&&BZOJ_4170_极光_KDTree
BZOJ_2989_数列&&BZOJ_4170_极光_KDTree Description "若是万一琪露诺(俗称rhl)进行攻击,什么都好,冷静地回答她的问题来吸引她.对方 ...
- ABP 框架启程 及 ABP 翻译目录及传送门
准备动手写一套电商的系统,辗转收集了不少相关的开源项目,最后决定使用ABP作为起点. 在园子里好多人都在推广ABP.有个园友做了一个集合贴,方便大家使用 ABP集合贴 建议大家优先看 HK Zhan ...
- HDU2190
悼念512汶川大地震遇难同胞——重建希望小学 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- Array 对象
Array的对象用于在单个的变量中存储多个值. constructor 返回对创建此对象的数组函数的引用. demo: let arr=[]; arr.constructor==Array let ...
- eclipse编辑窗口不见了(打开左边的java、xml文件,中间不会显示代码)
转自:https://blog.csdn.net/u012062810/article/details/46729779?utm_source=blogxgwz4 1. windows-->re ...
- linux进程状态D和Z的处理
长期生活在 Linux 环境里,渐渐地就有一种环保意识油然而生.比如,我们会在登录提示里写上"悟空,我跟你说过叫你不要乱扔东西,乱扔东西是不对的.哎呀我话没说完你怎么把 棍子扔掉了?月光宝盒 ...
- Android开发--Activity
一:Activity生命周期 (1)Activity生命周期中的几种方法: protected void onCreate(Bundle savedInstanceState): protected ...
- Using MultiPath TCP to enhance home networks
Over the last few months I’ve been playing with MultiPath TCP and in this post I will show how I use ...