当前位置:网站首页>leetcode:1139. 最大的以 1 为边界的正方形
leetcode:1139. 最大的以 1 为边界的正方形
2022-07-05 20:43:00 【OceanStar的学习笔记】
题目来源
题目描述

class Solution {
public:
int largest1BorderedSquare(vector<vector<int>>& grid) {
}
};
举个例子:可以肉眼看出一个满足条件的数组边长为 33 ,同时也是最大边长。
vector<vector<int>> grid ={
{
0, 1, 0, 1, 1},
{
1, 1, 1, 1, 1},
{
1, 1, 0, 1, 1},
{
1, 1, 1, 1, 0},
{
0, 1, 1, 1, 1},
{
1, 1, 1, 0, 1},
{
0, 1, 1, 1, 1},
{
1, 1, 1, 0, 1}
};
std::vector<std::vector<int>> grid = {
{
1, 1, 1},
{
1, 0, 1},
{
1, 1, 1}
};
题目解析
动态规划
class Solution {
public:
// 运用动态规划的思想,建立一个三维数组来存储每个点的左侧和上方连续的1的个数
int largest1BorderedSquare(vector<vector<int>>& grid) {
if(grid.empty()){
return 0;
}
int length = grid.size(); //矩阵的长
int width = grid[0].size(); //矩阵的宽
//用dp[i][j][0]来表示第i行第j列的 左边 连续的1的个数
//用dp[i][j][1]来表示第i行第j列的 上面 连续的1的个数
std::vector<std::vector<std::vector<int>>> dp(
length + 1, std::vector<std::vector<int>>(
width + 1, std::vector<int>(
2, 0
)
)
);
int maxLen = 0;
for (int i = 1; i <= length; ++i) {
for (int j = 1; j <= width; ++j) {
if(grid[i - 1][j - 1] == 1){
//因为上面的代码 i 和 j 都是从1开始,到m,n结束,可以理解为在矩形的左边和上边加了一层0,所以实际的dp[i][j][0]其实是grid[i-1][j-1]左边连续的1的个数(包括自身),这样做其实就是为了方便初始化,避免写太多的if-else,是一种很常见的技巧
dp[i][j][0] += dp[i][j - 1][0] + 1;
dp[i][j][1] += dp[i - 1][j][1] + 1;
//尝试以第i行第j列(当前点)为右下角构成正方形
int len = std::min(dp[i][j][0], dp[i][j][1]); //最大可能长度
while (len > 0){
//判断这个可能的正方形右上角左侧是否有连续len个1 && 左下角的上方是否有连续len个1
if(dp[i - len + 1][j][0] >= len && dp[i][j - len + 1][1] >= len){
break;
}
len--;
}
maxLen = std::max(maxLen, len);
}
}
}
return maxLen * maxLen;
}
};
边栏推荐
- 3.3、项目评估
- Y57. Chapter III kubernetes from entry to proficiency -- business image version upgrade and rollback (30)
- Implementation of redis unique ID generator
- 王老吉药业“关爱烈日下最可爱的人”公益活动在南京启动
- Abnova丨E (DIII) (WNV) 重组蛋白 中英文说明书
- Model method
- Is it safe to open an account online? Where can I get a low commission?
- ProSci LAG-3 重组蛋白说明书
- Usaco3.4 "broken Gong rock" band raucous rockers - DP
- 表单文本框的使用(二) 输入过滤(合成事件)
猜你喜欢

Norgen AAV提取剂盒说明书(含特色)

教你自己训练的pytorch模型转caffe(三)

【刷题记录】1. 两数之和

Cutting edge technology for cultivating robot education creativity

教你自己训练的pytorch模型转caffe(二)

Chemical properties and application instructions of prosci Lag3 antibody

ProSci LAG-3 重组蛋白说明书

Abnova丨E (DIII) (WNV) 重组蛋白 中英文说明书

解析创客教育的知识迁移和分享精神

Make Jar, Not War
随机推荐
解析五育融合之下的steam教育模式
Point cloud file Dat file read save
【UE4】UnrealInsight获取真机性能测试报告
Abbkine丨TraKine F-actin染色试剂盒(绿色荧光)方案
Classic implementation of the basic method of intelligent home of Internet of things
Pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs
Informatics Olympiad 1337: [example 3-2] word search tree | Luogu p5755 [noi2000] word search tree
Mathematical analysis_ Notes_ Chapter 9: curve integral and surface integral
[Yugong series] go teaching course in July 2022 004 go code Notes
Abnova blood total nucleic acid purification kit pre installed relevant instructions
3.3、项目评估
Graph embedding learning notes
Abbkine BCA法 蛋白质定量试剂盒说明书
phpstudy小皮的mysql点击启动后迅速闪退,已解决
PHP反序列化+MD5碰撞
Abnova fluorescent dye 620-m streptavidin scheme
清除app data以及获取图标
1. Strengthen learning basic knowledge points
14、Transformer--VIT TNT BETR
Propping of resources