当前位置:网站首页>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;
}
};
边栏推荐
- Is it necessary for bazel to learn
- 使用WebAssembly在浏览器端操作Excel
- 解读协作型机器人的日常应用功能
- phpstudy小皮的mysql点击启动后迅速闪退,已解决
- systemd-resolved 开启 debug 日志
- 学习机器人无从下手?带你体会当下机器人热门研究方向有哪些
- The development of research tourism practical education helps the development of cultural tourism industry
- vant 源码解析 event.ts 事件处理 全局函数 addEventListener详解
- PVC 塑料片BS 476-6 火焰传播性能测定
- LeetCode_ Hash table_ Difficulties_ 149. Maximum number of points on the line
猜你喜欢

The development of research tourism practical education helps the development of cultural tourism industry

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

Mathematical analysis_ Notes_ Chapter 9: curve integral and surface integral

2.<tag-哈希表, 字符串>补充: 剑指 Offer 50. 第一个只出现一次的字符 dbc

Abbkine trakine F-actin Staining Kit (green fluorescence) scheme
![[case] Application of positioning - Taobao rotation map](/img/2d/c834ce95a2c8e53a20e67fa2e99439.png)
[case] Application of positioning - Taobao rotation map

Norgen AAV extractant box instructions (including features)

leetcode:1755. 最接近目标值的子序列和

Using webassembly to operate excel on the browser side

CLion配置visual studio(msvc)和JOM多核编译
随机推荐
如何让化工企业的ERP库存账目更准确
Determine the best implementation of horizontal and vertical screens
MySQL ifnull usage function
Analyze the knowledge transfer and sharing spirit of maker Education
ODPS 下一个map / reduce 准备
Utils/index TS tool function
判断横竖屏的最佳实现
使用WebAssembly在浏览器端操作Excel
R语言【数据管理】
产品好不好,谁说了算?Sonar提出分析的性能指标,帮助您轻松判断产品性能及表现
Implementation of redis unique ID generator
Clear app data and get Icon
Abnova CRISPR spcas9 polyclonal antibody protocol
CareerCup它1.8 串移包括问题
字典树简单入门题(居然是蓝题?)
Material Design组件 - 使用BottomSheet展现扩展内容(二)
【案例】定位的运用-淘宝轮播图
基于flask写一个接口
培养机器人教育创造力的前沿科技
How to make ERP inventory accounts of chemical enterprises more accurate