[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 ...
随机推荐
- 什么是Maven?
Maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具. 发文时,绝大多数开发人员都把 Ant 当作 Java 编程项目的标准构建工具.遗憾的是, ...
- 谋哥:搞APP,做得累的都不对!
最近谋哥(微信viyi88)我刚加入“秦王会”,思想收到猛烈地冲击,各位大佬的思维有时候会让我大脑短路,收获不少.同时,我也慢慢发现我一直平静的 心开始浮躁,我发现苗头不对,于是开始静下心来.静下心, ...
- TOJ 3581: 最简IPv6表示
3581: 最简IPv6表示 Time Limit(Common/Java):1000MS/3000MS Memory Limit:65536KByteTotal Submit: 121 ...
- maven项目打包jar,含有依赖jar
在pom文件中添加一下插件 <plugin> <artifactId>maven-assembly-plugin</artifactId> <configur ...
- DefaultTransactionStatus源码
package org.springframework.transaction.support; import org.springframework.transaction.NestedTransa ...
- POJ 2051 Argus
Argus Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8782 Accepted: 3985 Description ...
- ThinkPHP5杂技(一)
Thinkphp5 assign 传递 " 时 ,前台收到的是 " 和ThinkPHP3.2不一样,3.2收到的是 ”,传递给js时 用的data.replace(new RegE ...
- hibernate悲观锁,乐观锁
业务逻辑的实现过程中,往往需要保证数据访问的排他性.因此,我们就需要通过一些机制来保证这些数据在某个操作过程中不会被外界修改,这样的机制,在这里,也就是所谓的“锁”,即给我们选定的目标数据上锁,使其无 ...
- Linux Shell系列教程之(十一)Shell while循环
本文是Linux Shell系列教程的第(十一)篇,更多Linux Shell教程请看:Linux Shell系列教程 在上一篇Linux Shell系列教程之(十)Shell for循环中,我们已经 ...
- ubuntu 安装tomcat<服务器>
一.下载tomcat 可以先下载到本地,然后ftp到服务器 官方 Apache Tomcat 的下载页面(下面的链接是apache自己的镜像服务器的地址,不同网络连接的话,apache会给出不同的镜像 ...