当前位置:网站首页>Likou 221 questions, the largest square
Likou 221 questions, the largest square
2022-08-10 21:26:00 【Yingtai Night Snow】
力扣221题,最大正方形
题目描述
在一个由 '0'
和 '1'
组成的二维矩阵内,找到只包含 '1'
的最大正方形,并返回其面积.
输入输出样例
输入:matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
输出:4
输入:matrix = [["0","1"],["1","0"]]
输出:1
输入:matrix = [["0"]]
输出:0
解法一:暴力解法
//使用暴力解法
//Iterates over each element in the matrix each time it is encountered1,That element is the upper left corner of the square
//Determine the upper left corner of the square,根据左上角所在的行和列计算可能的最大正方形的边长
//判断右下,右,Whether the following is an element1
//时间复杂度为O(mn*min(m,n)^2)
//空间复杂度 为 O(1)
int maximalSquare(vector<vector<char>>&matrix)
{
int rowLength=matrix.size();
int colLength=matrix[0].size();
if(rowLength==0||colLength==0)
{
return 0;
}
//The maximum side length for storing the current value
int maxSide=0;
//Iterate over every space in the space1的结点
for(int i=0;i<rowLength;i++)
{
for(int j=0;j<colLength;j++)
{
if(matrix[i][j]=='1')
{
maxSide=max(maxSide,1);
//Calculates the current maximum possible edge length
int currMaxSide=min(rowLength-i,colLength-j);
//Check whether the value in the lower right exists
for(int k=1;k<currMaxSide;k++)
{
//设置标志位,Indicates the current lower right,右侧,Whether the lower side is both1
bool flag=true;
if(matrix[i+k][j+k]=='0')
{
break;
}
//Bottom left verification is successful,then verify that the right and lower sides are1
for(int m=0;m<k;m++)
{
if(matrix[i+k][j+m]=='0'||matrix[i+m][j+k]=='0')
{
flag=false;
break;
}
}
if(flag)
{
maxSide=max(maxSide,k+1);
}
else
{
break;
}
}
}
}
}
int maxSquare=maxSide*maxSide;
return maxSquare;
}
解法二:使用动态规划
//使用动态规划的方法
//动态规划,Take the lower right corner as the initial value condition,且只包含1的正方形的边长的最大值
//状态转移方程 dp[i]由上方,左方,左上方的dp值决定
//dp[i][j]=min(dp[i-1][j],dp[i-1][j-1],dp[i][j-1])+1
//时复:O(mn)
//空复:O(mn)
int maximalSquare2(vector<vector<char>>&matrix)
{
int rowLength=matrix.size();
int colLength=matrix[0].size();
if(rowLength==0||colLength==0)
{
return 0;
}
//建立动态规划数组
vector<vector<int>>dp(rowLength,vector<int>(colLength,0));
//Initialize dynamic programming arrays and transition equations
int maxSides=0;
for(int row=0;row<rowLength;row++)
{
for(int col=0;col<colLength;col++)
{
if(matrix[row][col]=='1')
{
//对row和coldifferent locations for classification discussions
//因为在第一行,The first line is whateverdp的值肯定为1
if(row==0||col==0)
{
dp[row][col]=1;
}
else{
//转移方程
dp[row][col]=min(dp[row-1][col-1],min(dp[row-1][col],dp[row][col-1]))+1;
}
maxSides=max(maxSides,dp[row][col]);
}
}
}
return maxSides*maxSides;
}
边栏推荐
猜你喜欢
随机推荐
PostgreSQL — Installation and Common Commands
2022.8.9 模拟赛
B. Same Parity Summands
国内Gravatar头像的完美替代方案Cravatar
Auto.js中APP应用相关指令
Future-oriented IT infrastructure management architecture - Unified IaaS
2021DASCTF实战精英夏令营暨DASCTF July X CBCTF 4th
Single-click to cancel the function
Kubernetes 笔记 / 入门 / 生产环境 / 用部署工具安装 Kubernetes / 用 kubeadm 启动集群 / 用 kubeadm 创建集群
sklearn 笔记 TSNE
华为路由器旁挂引流实验(使用流策略)
函数:函数删除操作语法&使用例——《mysql 从入门到内卷再到入土》
C. Social Distance
【go】依赖注入
[Golang]从0到1写一个web服务(上)
ctfshow-osint
The use of TortoiseSVN little turtle
npm warn config global `--global`, `--local` are deprecated. use `--location=global` instead.
2021 CybricsCTF
Redis Command Manual