当前位置:网站首页>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;
};
边栏推荐
- C语言深度解剖——C语言关键字
- Hutool gracefully parses URL links and obtains parameters
- Tdengine biweekly selection of community issues | phase III
- Deep anatomy of C language -- C language keywords
- Tcp/ip protocol
- Export IEEE document format using latex
- What are the common processes of software stress testing? Professional software test reports issued by companies to share
- @Jsonbackreference and @jsonmanagedreference (solve infinite recursion caused by bidirectional references in objects)
- Process of obtaining the electronic version of academic qualifications of xuexin.com
- Niuke winter vacation training 6 maze 2
猜你喜欢
Mobile phones and computers on the same LAN access each other, IIS settings
opencv+dlib实现给蒙娜丽莎“配”眼镜
After reading the programmer's story, I can't help covering my chest...
Mongodb installation and basic operation
【ROS】usb_ Cam camera calibration
Indentation of tabs and spaces when writing programs for sublime text
软件卸载时遇到trying to use is on a network resource that is unavailable
Problems encountered in connecting the database of the project and their solutions
Problems in loading and saving pytorch trained models
Variable length parameter
随机推荐
Li Kou daily question 1 (2)
LeetCode:剑指 Offer 42. 连续子数组的最大和
LeetCode:剑指 Offer 48. 最长不含重复字符的子字符串
C language double pointer -- classic question type
What is the role of automated testing frameworks? Shanghai professional third-party software testing company Amway
Current situation and trend of character animation
Promise 在uniapp的简单使用
704 binary search
View computer devices in LAN
Warning in install. packages : package ‘RGtk2’ is not available for this version of R
软件压力测试常见流程有哪些?专业出具软件测试报告公司分享
数学建模2004B题(输电问题)
sublime text没关闭其他运行就使用CTRL+b运行另外的程序问题
Deep anatomy of C language -- C language keywords
SAP ui5 date type sap ui. model. type. Analysis of the parsing format of date
Super efficient! The secret of swagger Yapi
Variable length parameter
广州推进儿童友好城市建设,将探索学校周边200米设安全区域
LeetCode:836. 矩形重叠
Problems encountered in connecting the database of the project and their solutions