【 声明:版权全部,转载请标明出处。请勿用于商业用途。

联系信箱:libin493073668@sina.com】





题意:
给出一个仅仅包括0,1的二维矩阵。要求找到一个全为1的子矩阵。并输出子矩阵的面积

思路:
首先我们对这个矩阵进行求和
dp[i][j]表示以(1,1)为左上角。(i,j)为右下角的子矩阵的1的个数

如今我们要统计蓝色矩形的面积,如果右下角的坐标是(i,j)
此时蓝色矩形的宽与长各自是r,c
那么我们仅仅须要推断蓝色矩形内的1的个数是否与r*c相等,就能够知道这个矩形是否是全1子矩阵
那么怎么统计呢?
我们能够知道dp[i][j]是(1,1)到(i,j)的1的总个数
如果绿色矩阵的左上角是(0,0)
那么绿色矩阵的面积dp[i-r][j-c]
两个红色矩阵的面积各自是dp[i][j-c],dp[i-r][j]
那么统计蓝色矩阵1的个数的式子便是dp[i][j]-dp[i][j-c]-dp[i-r][j]+dp[i-r][j-c]
然后我们仅仅须要以每一个1为右下角,去增大r,c便可


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的更多相关文章

  1. 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 ...

  2. 85. Maximal Rectangle

    85. Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle c ...

  3. 刷题85. Maximal Rectangle

    一.题目说明 题目,85. Maximal Rectangle,计算只包含1的最大矩阵的面积.难度是Hard! 二.我的解答 看到这个题目,我首先想到的是dp,用dp[i][j]表示第i行第j列元素向 ...

  4. 求解最大矩形面积 — leetcode 85. Maximal Rectangle

    之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...

  5. 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 ...

  6. 【LeetCode】85. Maximal Rectangle

    Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...

  7. 【leetcode】85. Maximal Rectangle(单调栈)

    Given a rows x cols binary matrix filled with 0's and 1's, find the largest rectangle containing onl ...

  8. LeetCode (85): Maximal Rectangle [含84题分析]

    链接: https://leetcode.com/problems/maximal-rectangle/ [描述] Given a 2D binary matrix filled with '0's ...

  9. 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 ...

随机推荐

  1. 深入理解Java虚拟机(精华总结)

    作者:战斗民族就是干 转自:http://www.cnblogs.com/prayers/p/5515245.html 一.运行时数据区域 Java虚拟机管理的内存包括几个运行时数据内存:方法区.虚拟 ...

  2. Spring学习总结(20)——Spring加载多个项目properties配置文件问题解决

    多数的鲜为人知方法都是因为有着罕见的应用,就比如说Spring中PropertyPlaceholderConfigurer这个类,它是用来解析Java Properties属性文件值,并提供在spri ...

  3. Linux安装Scala

    下载Scala地址http://downloads.typesafe.com/scala/2.10.6/scala-2.10.6.tgz然后解压Scala到指定目录 tar -zxvf scala-2 ...

  4. 使用phppgadmin 遇到的小问题

    无法登录,显示错误消息如下: Error:login disallowed for security reasons. 解决方法: 修改conf/config.inc.php文件中的extra_log ...

  5. Maven之scope详解

    scope的分类 compile(编译范围) 默认就是compile,什么都不配置也就是意味着compile.compile表示被依赖项目需要参与当前项目的编译,当然后续的测试, 运行周期也参与其中, ...

  6. phpcms 后台也名称

    announce 公告 show.html 内容页 comment 评论 show_list.html 内容页评论列表 list.html 评论列表 content 内容模型 category.htm ...

  7. maven无法下载依赖jar包—几种仓库的区别

    一.问题背景 最近这两天,感觉自己智商急剧退化,到了自己都捉急的地步,呃,有必要记录下来,以后智商被人甩几条街的时候,看看这篇文字,找找灵感也是好的! 这个项目呢,是用IDEA开发的,我一切都弄好了, ...

  8. 解决webstorm安装babel卡死问题

    2017.07.16 现在大家可以直接使用yarn的方式安装包,可以解决babel目录过长导致webstorm卡死的问题. yarn的安装不会执行组件命令就不会导致node_modules下面继续嵌套 ...

  9. 刷题总结——运输计划(bzoj4326)

    题目: 题目背景 NOIP2015 提高组 Day2 T3 题目描述 公元2044年,人类进入了宇宙纪元. L国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之间,这 n-1 条航道 ...

  10. spring的事务传播与隔离

    propagation 事务的传播属性: 1.PROPAGATION_REQUIRED(*-required):支持当前事务,如果当前没有事务,就新建一个事务.(最常见的选择) 2.PROPAGATI ...