[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 ...
随机推荐
- 深入理解Java虚拟机(精华总结)
作者:战斗民族就是干 转自:http://www.cnblogs.com/prayers/p/5515245.html 一.运行时数据区域 Java虚拟机管理的内存包括几个运行时数据内存:方法区.虚拟 ...
- Spring学习总结(20)——Spring加载多个项目properties配置文件问题解决
多数的鲜为人知方法都是因为有着罕见的应用,就比如说Spring中PropertyPlaceholderConfigurer这个类,它是用来解析Java Properties属性文件值,并提供在spri ...
- Linux安装Scala
下载Scala地址http://downloads.typesafe.com/scala/2.10.6/scala-2.10.6.tgz然后解压Scala到指定目录 tar -zxvf scala-2 ...
- 使用phppgadmin 遇到的小问题
无法登录,显示错误消息如下: Error:login disallowed for security reasons. 解决方法: 修改conf/config.inc.php文件中的extra_log ...
- Maven之scope详解
scope的分类 compile(编译范围) 默认就是compile,什么都不配置也就是意味着compile.compile表示被依赖项目需要参与当前项目的编译,当然后续的测试, 运行周期也参与其中, ...
- phpcms 后台也名称
announce 公告 show.html 内容页 comment 评论 show_list.html 内容页评论列表 list.html 评论列表 content 内容模型 category.htm ...
- maven无法下载依赖jar包—几种仓库的区别
一.问题背景 最近这两天,感觉自己智商急剧退化,到了自己都捉急的地步,呃,有必要记录下来,以后智商被人甩几条街的时候,看看这篇文字,找找灵感也是好的! 这个项目呢,是用IDEA开发的,我一切都弄好了, ...
- 解决webstorm安装babel卡死问题
2017.07.16 现在大家可以直接使用yarn的方式安装包,可以解决babel目录过长导致webstorm卡死的问题. yarn的安装不会执行组件命令就不会导致node_modules下面继续嵌套 ...
- 刷题总结——运输计划(bzoj4326)
题目: 题目背景 NOIP2015 提高组 Day2 T3 题目描述 公元2044年,人类进入了宇宙纪元. L国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之间,这 n-1 条航道 ...
- spring的事务传播与隔离
propagation 事务的传播属性: 1.PROPAGATION_REQUIRED(*-required):支持当前事务,如果当前没有事务,就新建一个事务.(最常见的选择) 2.PROPAGATI ...