当前位置:网站首页>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;
};
边栏推荐
- pytorch查看张量占用内存大小
- [today in history] February 13: the father of transistors was born The 20th anniversary of net; Agile software development manifesto was born
- 超高效!Swagger-Yapi的秘密
- 力扣每日一题(二)
- @Jsonbackreference and @jsonmanagedreference (solve infinite recursion caused by bidirectional references in objects)
- Deep anatomy of C language -- C language keywords
- 企微服务商平台收费接口对接教程
- Export IEEE document format using latex
- [sword finger offer] serialized binary tree
- pytorch训练好的模型在加载和保存过程中的问题
猜你喜欢

egg. JS getting started navigation: installation, use and learning

egg. JS project deployment online server

Crash problem of Chrome browser

sublime text中conda环境中plt.show无法弹出显示图片的问题
![[MySQL] multi table query](/img/eb/9d54df9a5c6aef44e35c7a63b286a6.jpg)
[MySQL] multi table query

C language double pointer -- classic question type

UML diagram memory skills

ROS compilation calls the third-party dynamic library (xxx.so)

Visual implementation and inspection of visdom

Computer graduation design PHP Zhiduo online learning platform
随机推荐
poi追加写EXCEL文件
LeetCode:39. 组合总和
Visual implementation and inspection of visdom
Analysis of the source code of cocos2d-x for mobile game security (mobile game reverse and protection)
Light of domestic games destroyed by cracking
Problems encountered in connecting the database of the project and their solutions
Swagger setting field required is mandatory
CSP first week of question brushing
项目连接数据库遇到的问题及解决
Fairguard game reinforcement: under the upsurge of game going to sea, game security is facing new challenges
Problems in loading and saving pytorch trained models
Delay initialization and sealing classes
Computer graduation design PHP Zhiduo online learning platform
Export IEEE document format using latex
Trying to use is on a network resource that is unavailable
电脑F1-F12用途
Target detection - pytorch uses mobilenet series (V1, V2, V3) to build yolov4 target detection platform
LeetCode:836. 矩形重叠
Tdengine biweekly selection of community issues | phase III
MYSQL卸载方法与安装方法