当前位置:网站首页>leetcode:1139. The largest square bounded by 1
leetcode:1139. The largest square bounded by 1
2022-07-05 21:01:00 【Oceanstar's study notes】
Title source
Title Description
class Solution {
public:
int largest1BorderedSquare(vector<vector<int>>& grid) {
}
};
for instance : It can be seen with the naked eye that the side length of an array that meets the conditions is 33 , It is also the largest side length .
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}
};
title
Dynamic programming
class Solution {
public:
// Using the idea of dynamic planning , Create a three-dimensional array to store the left and top continuous 1 The number of
int largest1BorderedSquare(vector<vector<int>>& grid) {
if(grid.empty()){
return 0;
}
int length = grid.size(); // Length of matrix
int width = grid[0].size(); // The width of the matrix
// use dp[i][j][0] To represent the i Xing di j Column On the left Successive 1 The number of
// use dp[i][j][1] To represent the i Xing di j Column above Successive 1 The number of
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){
// Because the code above i and j from 1 Start , To m,n end , It can be understood as adding a layer on the left and top of the rectangle 0, So actually dp[i][j][0] It's actually grid[i-1][j-1] Continuous on the left 1 The number of ( Include yourself ), This is actually done to facilitate initialization , Avoid writing too much if-else, Is a very common skill
dp[i][j][0] += dp[i][j - 1][0] + 1;
dp[i][j][1] += dp[i - 1][j][1] + 1;
// Try to i Xing di j Column ( Current point ) Form a square for the lower right corner
int len = std::min(dp[i][j][0], dp[i][j][1]); // Maximum possible length
while (len > 0){
// Judge whether there is continuity on the left side of the upper right corner of this possible square len individual 1 && Whether there is continuity above the lower left corner len individual 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;
}
};
边栏推荐
- Vant source code parsing event Detailed explanation of TS event processing global function addeventlistener
- MYSQL IFNULL使用功能
- shell编程100例
- 教你自己训练的pytorch模型转caffe(三)
- PHP反序列化+MD5碰撞
- Traps in the explode function in PHP
- Modifiers of attributes of TS public, private, protect
- SYSTEMd resolved enable debug log
- MySQL 千万数据量深分页优化, 拒绝线上故障!
- 珍爱网微服务底层框架演进从开源组件封装到自研
猜你喜欢
10000+ 代码库、3000+ 研发人员大型保险集团的研发效能提升实践
Talk about my fate with some programming languages
浅聊我和一些编程语言的缘分
从架构上详解技术(SLB,Redis,Mysql,Kafka,Clickhouse)的各类热点问题
Abnova CRISPR spcas9 polyclonal antibody protocol
2. < tag hash table, string> supplement: Sword finger offer 50 The first character DBC that appears only once
Norgen AAV extractant box instructions (including features)
【案例】定位的运用-淘宝轮播图
Duchefa d5124 md5a medium Chinese and English instructions
[case] Application of positioning - Taobao rotation map
随机推荐
概率论机器学习的先验知识(上)
EN 438-7建筑覆盖物装饰用层压板材产品—CE认证
基于vertx-web-sstore-redis的改造实现vertx http应用的分布式session
Popular science | does poor English affect the NPDP exam?
珍爱网微服务底层框架演进从开源组件封装到自研
R语言【数据管理】
Viewrootimpl and windowmanagerservice notes
序列联配Sequence Alignment
phpstudy小皮的mysql点击启动后迅速闪退,已解决
Binary search
基于AVFoundation实现视频录制的两种方式
leetcode:1139. 最大的以 1 为边界的正方形
Clear app data and get Icon
Abnova CRISPR spcas9 polyclonal antibody protocol
LeetCode: Distinct Subsequences [115]
Duchefa d5124 md5a medium Chinese and English instructions
ArcGIS栅格重采样方法介绍
Abnova maxpab mouse derived polyclonal antibody solution
Which is the best online collaboration product? Microsoft loop, notion, flowus
Learning notes of SAS programming and data mining business case 19