当前位置:网站首页>LeetCode:221. 最大正方形
LeetCode:221. 最大正方形
2022-07-06 08:44:00 【Bertil】
在一个由 ‘0’ 和 ‘1’ 组成的二维矩阵内,找到只包含 ‘1’ 的最大正方形,并返回其面积。
示例 1:

输入:matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
输出:4
示例 2:
输入:matrix = [["0","1"],["1","0"]]
输出:1
示例 3:
输入:matrix = [["0"]]
输出:0
提示:
- m == matrix.length
- n == matrix[i].length
- 1 <= m, n <= 300
- matrix[i][j] 为 ‘0’ 或 ‘1’
解题思路
1.首先定义一个数组dp表示以[i][j]为正方形右下角的最大边长,定义一个变量max_len表示最大正方形的边长
2.状态转移方程:dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]) + 1;(取三者的最小值是因为要构成正方形)
3.注意:若左边和上边的点为’1’,则dp[i][j] = 1
代码
/** * @param {character[][]} matrix * @return {number} */
var maximalSquare = function(matrix) {
let max_len = 0;
let dp = Array.from(Array(matrix.length), () => Array(matrix[0].length).fill(0));
for (let i = 0; i < matrix.length; i++) {
for (let j = 0; j < matrix[i].length; j++) {
if (matrix[i][j] === '1') {
if (i === 0 || j === 0) {
dp[i][j] = 1;
}else {
dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]) + 1;
}
max_len = Math.max(max_len, dp[i][j]);
}
}
}
return max_len ** 2;
};
边栏推荐
- Deep analysis of C language data storage in memory
- R language uses the principal function of psych package to perform principal component analysis on the specified data set. PCA performs data dimensionality reduction (input as correlation matrix), cus
- Analysis of the source code of cocos2d-x for mobile game security (mobile game reverse and protection)
- sublime text的编写程序时的Tab和空格缩进问题
- Colorlog combined with logging to print colored logs
- TP-LINK 企业路由器 PPTP 配置
- UnsupportedOperationException异常
- POI add write excel file
- egg. JS directory structure
- R language ggplot2 visualization, custom ggplot2 visualization image legend background color of legend
猜你喜欢

【嵌入式】Cortex M4F DSP库
![[embedded] cortex m4f DSP Library](/img/83/ab421d5cc18e907056ec2bdaeb7d5c.png)
[embedded] cortex m4f DSP Library

Guangzhou will promote the construction of a child friendly city, and will explore the establishment of a safe area 200 meters around the school

Promise 在uniapp的简单使用

Swagger setting field required is mandatory

Sublime text using ctrl+b to run another program without closing other runs

软件卸载时遇到trying to use is on a network resource that is unavailable

Detailed explanation of heap sorting

JVM quick start

Deep anatomy of C language -- C language keywords
随机推荐
查看局域网中电脑设备
Indentation of tabs and spaces when writing programs for sublime text
如何进行接口测试测?有哪些注意事项?保姆级解读
Modify the video name from the name mapping relationship in the table
pytorch训练好的模型在加载和保存过程中的问题
[MySQL] log
Process of obtaining the electronic version of academic qualifications of xuexin.com
Variable length parameter
目标检测——Pytorch 利用mobilenet系列(v1,v2,v3)搭建yolov4目标检测平台
同一局域网的手机和电脑相互访问,IIS设置
TP-LINK 企业路由器 PPTP 配置
Research Report on supply and demand and development prospects of China's high purity aluminum market (2022 Edition)
Verrouillage [MySQL]
Unified ordering background interface product description Chinese garbled
游戏解包的危害及资源加密的重要性
Detailed explanation of heap sorting
China high purity silver nitrate Market Research and investment strategy report (2022 Edition)
sublime text中conda环境中plt.show无法弹出显示图片的问题
Navicat premium create MySQL create stored procedure
[NVIDIA development board] FAQ (updated from time to time)