[LeedCode OJ]#85 Maximal Rectangle
联系信箱:libin493073668@sina.com】
class Solution
{
public:
int maximalRectangle(vector<vector<char> >& matrix)
{
int n = matrix.size();
if(n==0) return 0;
int m = matrix[0].size();
int i,j,c;
vector<vector<int> > dp,a;
dp.resize(n+1),a.resize(n+1);
for(i = 0; i<=n; i++)
{
dp[i].resize(m+1);
a[i].resize(m+1);
}
for(i = 0; i<n; i++)
{
for(j = 0; j<m; j++)
{
a[i+1][j+1] = matrix[i][j]-'0';
}
}
int sum = 0;
//计算1的个数
for(i = 1; i<=m; i++)
{
sum+=a[1][i];
dp[1][i] = sum;
}
for(i = 2; i<=n; i++)
{
sum = 0;
for(j = 1; j<=m; j++)
{
sum+=a[i][j];
dp[i][j]=dp[i-1][j]+sum;
}
}
//以每一个1为右下角,寻找最大全1子矩阵
int maxn = 0;
for(i = n; i>0; i--)
{
for(j = m; j>0&&maxn<i*j; j--)
{
if(a[i][j])
{
int r = 1,c = 1;
while(j-c>=0)
{
if(dp[i][j]-dp[i][j-c]-dp[i-r][j]+dp[i-r][j-c]==r*c)//是全1矩阵。继续增大列
{
maxn = max(maxn,r*c);
c++;
}
else
break;
}
while(i-r>=0&&c>0)
{
if(dp[i][j]-dp[i][j-c]-dp[i-r][j]+dp[i-r][j-c]==r*c)//是全1矩阵。继续增大行
{
maxn = max(maxn,r*c);
r++;
}
else//否则。降低一列再去反复推断
c--;
}
}
}
}
return maxn;
}
};
[LeedCode OJ]#85 Maximal Rectangle的更多相关文章
- LeetCode OJ 85. Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and ...
- 85. Maximal Rectangle
85. Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle c ...
- 刷题85. Maximal Rectangle
一.题目说明 题目,85. Maximal Rectangle,计算只包含1的最大矩阵的面积.难度是Hard! 二.我的解答 看到这个题目,我首先想到的是dp,用dp[i][j]表示第i行第j列元素向 ...
- 求解最大矩形面积 — leetcode 85. Maximal Rectangle
之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...
- 85. Maximal Rectangle (Graph; Stack, DP)
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 Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...
- 【leetcode】85. Maximal Rectangle(单调栈)
Given a rows x cols binary matrix filled with 0's and 1's, find the largest rectangle containing onl ...
- LeetCode (85): Maximal Rectangle [含84题分析]
链接: https://leetcode.com/problems/maximal-rectangle/ [描述] Given a 2D binary matrix filled with '0's ...
- 84. Largest Rectangle in Histogram *HARD* -- 柱状图求最大面积 85. Maximal Rectangle *HARD* -- 求01矩阵中的最大矩形
1. Given n non-negative integers representing the histogram's bar height where the width of each bar ...
随机推荐
- loj2005 「SDOI2017」相关分析
鬼畜线段树--Orz Capella #include <iostream> #include <cstdio> using namespace std; int n, m, ...
- 设计模式之工厂模式 Factory实现
simpleFactory //car接口 public interface Car { void run(); } //两个实现类 public class Audi implements Car{ ...
- EF知识和经验
AsNoTracking提高查询性能 AsNoTracking的作用就是在查询的时候不做追踪,这样会查询的更快,但是这样做会有一个缺陷(不能对查询的数据做修改操作). var student2 = d ...
- poj1985&&第四次CCF软件认证第4题 求树的直径
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 4216 Accepted: 2137 Case ...
- 九度oj 题目1380:lucky number
题目描述: 每个人有自己的lucky number,小A也一样.不过他的lucky number定义不一样.他认为一个序列中某些数出现的次数为n的话,都是他的lucky number.但是,现在这个序 ...
- 【bzoj】P4407于神之怒加强版(莫比乌斯反演)
题目链接 套路一般的枚举$gcd(i,j)=w$.设$min(n,m)=top$,则有 $\sum\limits_{i=1}^{n}\sum\limits_{j=1}{m}gcd(i,j)$ $=\s ...
- ida动态调试笔记
ida动态调试笔记 目标文件:阿里安全挑战赛的第二题 点击打开链接 使用环境:ida6.8点击打开链接,adt bundle点击打开链接 首先打开avd安卓模拟器,界面如下: 在dos下运行adb命令 ...
- 背包问题--golang的入门解
. 定义一种 表示静态属性的 关键字 / 修饰符 2. 作用共用.共享 能有此作用的原因分析: Java中,任何变量 / 代码存储时,都是 在编译时 由系统自动分配内存在静态变量编译后,所分配的内存会 ...
- from __future__ import unicode_literals, absolute_import
Q:python模块中的相对导入,绝对导入,有些地方会写 from __future__ import absolute_import 希望有个更详细的讲解. A: 相对导入:在不指明 package ...
- bzoj 2794 [Poi2012]Cloakroom 离线+背包
题目大意 有n件物品,每件物品有三个属性a[i], b[i], c[i] (a[i]<b[i]). 再给出q个询问,每个询问由非负整数m, k, s组成,问是否能够选出某些物品使得: 对于每个选 ...