当前位置:网站首页>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;
};
边栏推荐
- Navicat Premium 创建MySql 创建存储过程
- What is the role of automated testing frameworks? Shanghai professional third-party software testing company Amway
- 数学建模2004B题(输电问题)
- 企微服务商平台收费接口对接教程
- 个人电脑好用必备软件(使用过)
- 随手记01
- 深度剖析C语言数据在内存中的存储
- 优秀的软件测试人员,都具备这些能力
- Problems in loading and saving pytorch trained models
- Delay initialization and sealing classes
猜你喜欢

Deep anatomy of C language -- C language keywords

C语言双指针——经典题型

Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

LeetCode:124. 二叉树中的最大路径和

Crash problem of Chrome browser

Restful API design specification

目标检测——Pytorch 利用mobilenet系列(v1,v2,v3)搭建yolov4目标检测平台

pytorch训练好的模型在加载和保存过程中的问题

Computer cleaning, deleted system files
随机推荐
LeetCode:剑指 Offer 42. 连续子数组的最大和
Detailed explanation of dynamic planning
Leetcode刷题题解2.1.1
有效提高软件产品质量,就找第三方软件测评机构
C語言雙指針——經典題型
深度剖析C语言数据在内存中的存储
TCP/IP协议
Tdengine biweekly selection of community issues | phase III
Sublime text using ctrl+b to run another program without closing other runs
Tcp/ip protocol
LeetCode:劍指 Offer 42. 連續子數組的最大和
电脑F1-F12用途
Alibaba cloud server mining virus solution (practiced)
超高效!Swagger-Yapi的秘密
Current situation and trend of character animation
目标检测——Pytorch 利用mobilenet系列(v1,v2,v3)搭建yolov4目标检测平台
Restful API design specification
LeetCode:394. 字符串解码
LeetCode:剑指 Offer 48. 最长不含重复字符的子字符串
opencv+dlib实现给蒙娜丽莎“配”眼镜