当前位置:网站首页>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;
}
};
边栏推荐
- MySQL InnoDB架构原理
- Point cloud file Dat file read save
- E. Singhal and numbers (prime factor decomposition)
- 10000+ 代码库、3000+ 研发人员大型保险集团的研发效能提升实践
- 清除app data以及获取图标
- 科普|英语不好对NPDP考试有影响吗 ?
- Mathematical analysis_ Notes_ Chapter 9: curve integral and surface integral
- Applet project structure
- 研學旅遊實踐教育的開展助力文旅產業發展
- Applet global configuration
猜你喜欢

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

Duchefa丨MS培养基含维生素说明书
MySQL fully parses json/ arrays

How to form standard interface documents

2022 Beijing eye health products exhibition, eye care products exhibition, China eye Expo held in November

Abnova DNA marker high quality control test program

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

1. Strengthen learning basic knowledge points

Duchefa s0188 Chinese and English instructions of spectinomycin hydrochloride pentahydrate

【刷题记录】1. 两数之和
随机推荐
Norgen AAV提取剂盒说明书(含特色)
Abnova CRISPR spcas9 polyclonal antibody protocol
【UE4】UnrealInsight获取真机性能测试报告
14、Transformer--VIT TNT BETR
Duchefa丨D5124 MD5A 培养基中英文说明书
小程序项目结构
1、强化学习基础知识点
Document method
Informatics Olympiad 1337: [example 3-2] word search tree | Luogu p5755 [noi2000] word search tree
2.8 basic knowledge of project management process
Abnova丨CRISPR SpCas9 多克隆抗体方案
渗透创客精神文化转化的创客教育
Wanglaoji pharmaceutical's public welfare activity of "caring for the most lovely people under the scorching sun" was launched in Nanjing
phpstudy小皮的mysql点击启动后迅速闪退,已解决
小程序事件绑定
Abnova丨E (DIII) (WNV) 重组蛋白 中英文说明书
Use of thread pool
E. Singhal and numbers (prime factor decomposition)
Fundamentals - configuration file analysis
Clear app data and get Icon