当前位置:网站首页>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;
}
};
边栏推荐
- Graph embedding learning notes
- Abnova丨E (DIII) (WNV) 重组蛋白 中英文说明书
- Maker education infiltrating the transformation of maker spirit and culture
- Abnova CD81 monoclonal antibody related parameters and Applications
- National Eye Care Education Conference, 2022 the Fourth Beijing International Youth eye health industry exhibition
- Specification of protein quantitative kit for abbkine BCA method
- 中国管理科学研究院凝聚行业专家,傅强荣获智库专家“十佳青年”称号
- Point cloud file Dat file read save
- How to make ERP inventory accounts of chemical enterprises more accurate
- Mongodb basic exercises
猜你喜欢

Norgen AAV extractant box instructions (including features)

Introduction to dead letter queue (two consumers, one producer)

IC popular science article: those things about Eco

Open source SPL eliminates tens of thousands of database intermediate tables

PHP反序列化+MD5碰撞

当Steam教育进入个性化信息技术课程

Abnova丨荧光染料 620-M 链霉亲和素方案

Chemical properties and application instructions of prosci Lag3 antibody

MySQL InnoDB架构原理

Use of thread pool
随机推荐
Abbkine BCA法 蛋白质定量试剂盒说明书
产品好不好,谁说了算?Sonar提出分析的性能指标,帮助您轻松判断产品性能及表现
Chemical properties and application instructions of prosci Lag3 antibody
How to form standard interface documents
珍爱网微服务底层框架演进从开源组件封装到自研
2020 CCPC Weihai - A. golden spirit (thinking), D. ABC project (big number decomposition / thinking)
Abnova丨 CD81单克隆抗体相关参数和应用
研學旅遊實踐教育的開展助力文旅產業發展
ProSci LAG3抗体的化学性质和应用说明
资源道具化
小程序页面导航
小程序代码的构成
Leetcode (695) - the largest area of an island
Abnova maxpab mouse derived polyclonal antibody solution
Abnova CRISPR spcas9 polyclonal antibody protocol
Pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs
Abnova DNA marker high quality control test program
mysql全面解析json/数组
Use of thread pool
14、Transformer--VIT TNT BETR