当前位置:网站首页>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;
}
};
边栏推荐
- 挖财商学院给的证券账户安全吗?可以开户吗?
- Norgen AAV extractant box instructions (including features)
- Applet global configuration
- 插值查找的简单理解
- Chemical properties and application instructions of prosci Lag3 antibody
- Model method
- Duchefa p1001 plant agar Chinese and English instructions
- Fundamentals - configuration file analysis
- phpstudy小皮的mysql点击启动后迅速闪退,已解决
- Mongodb/ document operation
猜你喜欢
小程序事件绑定
MySQL InnoDB架构原理
Duchefa low melting point agarose PPC Chinese and English instructions
AI automatically generates annotation documents from code
[record of question brushing] 1 Sum of two numbers
教你自己训练的pytorch模型转caffe(三)
解读协作型机器人的日常应用功能
产品好不好,谁说了算?Sonar提出分析的性能指标,帮助您轻松判断产品性能及表现
Norgen AAV extractant box instructions (including features)
2.8 basic knowledge of project management process
随机推荐
Applet project structure
CCPC 2021 Weihai - G. shinyruo and KFC (combination number, tips)
mysql全面解析json/数组
1、强化学习基础知识点
phpstudy小皮的mysql点击启动后迅速闪退,已解决
重上吹麻滩——段芝堂创始人翟立冬游记
Analyze the knowledge transfer and sharing spirit of maker Education
Duchefa丨D5124 MD5A 培养基中英文说明书
Simple understanding of interpolation search
Document method
3.3 project evaluation
Specification of protein quantitative kit for abbkine BCA method
Wanglaoji pharmaceutical's public welfare activity of "caring for the most lovely people under the scorching sun" was launched in Nanjing
Is it safe to open an account online? Where can I get a low commission?
Is the securities account given by the school of Finance and business safe? Can I open an account?
Clear app data and get Icon
Chemical properties and application instructions of prosci Lag3 antibody
2.<tag-哈希表, 字符串>补充: 剑指 Offer 50. 第一个只出现一次的字符 dbc
欢迎来战,赢取丰厚奖金:Code Golf 代码高尔夫挑战赛正式启动
教你自己训练的pytorch模型转caffe(二)