dp.

#define MAX 1000
int rowLeft[MAX][MAX];
int colUp[MAX][MAX];
int dp[MAX][MAX];
void calRow(char **matrix,int matrixRowSize,int matrixColSize){
int i,j;
for(i=0;i<matrixRowSize;++i){
for(j=0;j<matrixColSize;++j)
{
if(matrix[i][j]=='1')
{
if(j==0)rowLeft[i][j]=1;
else rowLeft[i][j]=rowLeft[i][j-1]+1;
}
else rowLeft[i][j]=0;
}
}
}
void calCol(char **matrix,int matrixRowSize,int matrixColSize){
int i,j;
for(i=0;i<matrixRowSize;++i)
{
for(j=0;j<matrixColSize;++j)
{
if(matrix[i][j]=='1'){
if(i==0)colUp[i][j]=1;
else colUp[i][j]=colUp[i-1][j]+1;
}
else colUp[i][j]=0;
}
}
}
int min(int a,int b,int c){
int t;
t=(a<b?a:b);
t=(t<c?t:c);
return t;
}
int maximalSquare(char** matrix, int matrixRowSize, int matrixColSize) {
int i,j,maxSquare=0;
calRow(matrix,matrixRowSize,matrixColSize);
calCol(matrix,matrixRowSize,matrixColSize);
for(i=0;i<matrixRowSize;++i)
{ for(j=0;j<matrixColSize;++j)
{
if(i==0||j==0)
dp[i][j]=(matrix[i][j]=='1'?1:0);
else dp[i][j]=min(dp[i-1][j-1]+1,rowLeft[i][j],colUp[i][j]);
maxSquare=(maxSquare>dp[i][j]?maxSquare:dp[i][j]);
}
}
return maxSquare*maxSquare;
}

  

我这个空间复杂度有点高

Maximal Square || LeetCode的更多相关文章

  1. 求解最大正方形面积 — leetcode 221. Maximal Square

    本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...

  2. leetcode每日解题思路 221 Maximal Square

    问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到 ...

  3. 【动态规划】leetcode - Maximal Square

    称号: Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square contain ...

  4. LeetCode之“动态规划”:Maximal Square && Largest Rectangle in Histogram && Maximal Rectangle

    1. Maximal Square 题目链接 题目要求: Given a 2D binary matrix filled with 0's and 1's, find the largest squa ...

  5. 【LeetCode】221. Maximal Square

    Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...

  6. 【刷题-LeetCode】221. Maximal Square

    Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...

  7. [LintCode] Maximal Square 最大正方形

    Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...

  8. [LeetCode] Maximal Square 最大正方形

    Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...

  9. LeetCode Maximal Square

    原题链接在这里:https://leetcode.com/problems/maximal-square/ 这是一道DP题,存储历史信息是到当前点能有的最大square, 用二维数组dp存储. 更新方 ...

随机推荐

  1. Converting Stream to String and back…what are we missing?

    string test = "Testing 1-2-3"; // convert string to stream byte[] byteArray = Encoding.ASC ...

  2. CodeForces Round 199 Div2

    完了,这次做扯了,做的时候有点发烧,居然只做出来一道题,差点被绿. My submissions     # When Who Problem Lang Verdict Time Memory 443 ...

  3. BZOJ3853 : GCD Array

    1 n d v相当于给$a[x]+=v[\gcd(x,n)=d]$ \[\begin{eqnarray*}&&v[\gcd(x,n)=d]\\&=&v[\gcd(\fr ...

  4. BZOJ3547 : [ONTAK2010]Matchings

    树形DP f[i][0]表示不向下连边的最大匹配数 f[i][1]表示向下连一条边的最大匹配数 h[][]表示对应的方案数 为了防止爆栈用BFS 为了防止MLE: 1.数组循环利用,比如存边的数组在存 ...

  5. 各新旧版本Java及其相关文档可以从这里下载

    http://www.oracle.com/technetwork/java/archive-139210.html

  6. 什么情况下include_path不起作用?

    include_path是怎么起作用的? 如果有多个include_path顺序是怎么样的? 什么情况下include_path不起作用? 今天, 我就全面的介绍下这个问题, 先从一个例子开始吧. 如 ...

  7. Java下利用Jackson进行JSON解析和序列化

    Java下利用Jackson进行JSON解析和序列化   Java下常见的Json类库有Gson.JSON-lib和Jackson等,Jackson相对来说比较高效,在项目中主要使用Jackson进行 ...

  8. [转].net自定义验证控件CustomValidator的使用

    本文转自:http://tech.cncms.com/web/aspnet/96310.html CustomValidator验证控件,可以自定义验证函数,实现其它几个验证控件不能实现的验证规则,最 ...

  9. Embedded Database service fails to start after installing or migrating to Symantec Endpoint Protection (SEP) 12.1.5 (RU5)

    https://support.symantec.com/en_US/article.TECH225587.html

  10. Ural 1741 Communication Fiend(隐式图+虚拟节点最短路)

    1741. Communication Fiend Time limit: 1.0 second Memory limit: 64 MB Kolya has returned from a summe ...