Maximal Square || LeetCode
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的更多相关文章
- 求解最大正方形面积 — leetcode 221. Maximal Square
本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...
- leetcode每日解题思路 221 Maximal Square
问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到 ...
- 【动态规划】leetcode - Maximal Square
称号: Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square contain ...
- 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 ...
- 【LeetCode】221. Maximal Square
Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...
- 【刷题-LeetCode】221. Maximal Square
Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...
- [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 ...
- [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 ...
- LeetCode Maximal Square
原题链接在这里:https://leetcode.com/problems/maximal-square/ 这是一道DP题,存储历史信息是到当前点能有的最大square, 用二维数组dp存储. 更新方 ...
随机推荐
- RETINA显示屏下ICON优化方法
便于理解,先来了解几个名词: dpi(dots per inch),每英寸的点数,用来测量任何设备的硬件分辨率.一个21”的屏幕可以拥有1680 X 1050 的分辨率,27”的屏幕也可以拥有相同的分 ...
- 20145315 《Java程序设计》实验五实验报告
20145315 <Java程序设计>实验五实验报告 实验五 Java网络编程 我负责服务器部分,王嘉澜负责客户端部分:http://www.cnblogs.com/SJZGM10/p/5 ...
- 【BZOJ】2875: [Noi2012]随机数生成器(矩阵乘法+快速乘)
http://www.lydsy.com/JudgeOnline/problem.php?id=2875 矩阵的话很容易看出来.....我就不写了.太水了. 然后乘法longlong会溢出...那么我 ...
- SecureCrt自动化
Crt自动化 测试 SecureCrt脚本 JS脚本 引言 软件介绍 脚本介绍 引言 在嵌入式公司中,面对大量的网络设备,不论开发同事进行设备开发.测试同事进行大量测试工作还是运维人员进行大量设备 ...
- 再析在spring框架中解决多数据源的问题
在前面我写了<如何在spring框架中解决多数据源的问题>,通过设计模式中的Decorator模式在spring框架中解决多数据源的问题,得到了许多网友的关注.在与网友探讨该问题的过程中, ...
- 关于iOS测试机个数上限的详细规则
关于iOS测试机个数上限的详细规则 前言 公司的iOS测试机快达到苹果规定的100个上限了,而因为the new iPad新出,我们需要新的quota来测试新iPad,所以就仔细研究了一下苹果关于10 ...
- 怎样将文件夹打包为jar包或war包
在dos命令中,找到要打包文件夹位置,输入jar -cvf 打包后的名和格式 文件夹名 如图: 在我的文件夹的位置就能看到jar包和war包了
- Repair duplicate IDs on cloned Endpoint Protection 12.1 clients
https://support.symantec.com/en_US/article.TECH163349.html
- unity HideInInspector与SerializeField
[HideInInspector]表示将原本显示在面板上的序列化值隐藏起来. [SerializeField]表示将原本不会被序列化的私有变量和保护变量可以序列化,这么他们在下次读取时,就是你上次赋值 ...
- javaWeb中servlet开发(1)——helloworld
1.servlet 1.1 servlet简介 1.2 servlet流程 不管是servlet还是jsp,所有的程序都是在服务器端处理的,所以必须了解一个servlet基本流程 servlet和JS ...