当前位置:网站首页>LeetCode:221. Largest Square
LeetCode:221. Largest Square
2022-07-06 08:51:00 【Bertil】
In a ‘0’ and ‘1’ In the two-dimensional matrix , Found contains only ‘1’ The largest square of , And return its area .
Example 1:
Input :matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
Output :4
Example 2:
Input :matrix = [["0","1"],["1","0"]]
Output :1
Example 3:
Input :matrix = [["0"]]
Output :0
Tips :
- m == matrix.length
- n == matrix[i].length
- 1 <= m, n <= 300
- matrix[i][j] by ‘0’ or ‘1’
Their thinking
1. First define an array dp Said to [i][j] Is the maximum side length of the lower right corner of the square , Define a variable max_len Represents the side length of the largest square
2. State transition equation :dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]) + 1;( The minimum value of the three is taken because it is necessary to form a square )
3. Be careful : If the points on the left and top are ’1’, be dp[i][j] = 1
Code
/** * @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;
};
边栏推荐
- UML圖記憶技巧
- Image, CV2 read the conversion and size resize change of numpy array of pictures
- Introduction to the differences between compiler options of GCC dynamic library FPIC and FPIC
- C語言雙指針——經典題型
- LeetCode:387. 字符串中的第一个唯一字符
- Delay initialization and sealing classes
- marathon-envs项目环境配置(强化学习模仿参考动作)
- Tdengine biweekly selection of community issues | phase III
- swagger设置字段required必填
- pytorch训练好的模型在加载和保存过程中的问题
猜你喜欢
Computer graduation design PHP Zhiduo online learning platform
Current situation and trend of character animation
LeetCode:236. 二叉树的最近公共祖先
MongoDB 的安装和基本操作
深度剖析C语言数据在内存中的存储
Crash problem of Chrome browser
LeetCode:124. 二叉树中的最大路径和
数学建模2004B题(输电问题)
[embedded] print log using JLINK RTT
Compétences en mémoire des graphiques UML
随机推荐
项目连接数据库遇到的问题及解决
LeetCode:34. 在排序数组中查找元素的第一个和最后一个位置
Process of obtaining the electronic version of academic qualifications of xuexin.com
Purpose of computer F1-F12
Analysis of the source code of cocos2d-x for mobile game security (mobile game reverse and protection)
软件压力测试常见流程有哪些?专业出具软件测试报告公司分享
LeetCode:124. 二叉树中的最大路径和
[sword finger offer] serialized binary tree
[embedded] cortex m4f DSP Library
UnsupportedOperationException异常
Using C language to complete a simple calculator (function pointer array and callback function)
Pytorch view tensor memory size
软件卸载时遇到trying to use is on a network resource that is unavailable
Trying to use is on a network resource that is unavailable
[MySQL] limit implements paging
ROS compilation calls the third-party dynamic library (xxx.so)
Niuke winter vacation training 6 maze 2
Sublime text in CONDA environment plt Show cannot pop up the problem of displaying pictures
随手记01
Guangzhou will promote the construction of a child friendly city, and will explore the establishment of a safe area 200 meters around the school