当前位置:网站首页>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;
};
边栏推荐
- Detailed explanation of dynamic planning
- Bitwise logical operator
- Computer cleaning, deleted system files
- Mobile phones and computers on the same LAN access each other, IIS settings
- [MySQL] multi table query
- Target detection - pytorch uses mobilenet series (V1, V2, V3) to build yolov4 target detection platform
- Revit 二次开发 HOF 方式调用transaction
- LeetCode:236. 二叉树的最近公共祖先
- [MySQL] limit implements paging
- Restful API design specification
猜你喜欢

电脑清理,删除的系统文件

Using pkgbuild:: find in R language_ Rtools check whether rtools is available and use sys The which function checks whether make exists, installs it if not, and binds R and rtools with the writelines

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

C语言深度解剖——C语言关键字

同一局域网的手机和电脑相互访问,IIS设置

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

Navicat premium create MySQL create stored procedure

The harm of game unpacking and the importance of resource encryption

Excellent software testers have these abilities

MongoDB 的安装和基本操作
随机推荐
LeetCode:836. 矩形重叠
Image,cv2读取图片的numpy数组的转换和尺寸resize变化
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
The mysqlbinlog command uses
LeetCode:34. 在排序数组中查找元素的第一个和最后一个位置
What is the role of automated testing frameworks? Shanghai professional third-party software testing company Amway
Alibaba cloud server mining virus solution (practiced)
Charging interface docking tutorial of enterprise and micro service provider platform
Double pointeur en langage C - - modèle classique
Marathon envs project environment configuration (strengthen learning and imitate reference actions)
TDengine 社区问题双周精选 | 第三期
TCP/IP协议
C language double pointer -- classic question type
LeetCode:394. 字符串解码
SAP ui5 date type sap ui. model. type. Analysis of the parsing format of date
LeetCode:39. 组合总和
LeetCode:剑指 Offer 04. 二维数组中的查找
Visual implementation and inspection of visdom
Guangzhou will promote the construction of a child friendly city, and will explore the establishment of a safe area 200 meters around the school
自动化测试框架有什么作用?上海专业第三方软件测试公司安利