当前位置:网站首页>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;
};
边栏推荐
- 多元聚类分析
- Computer cleaning, deleted system files
- LeetCode:剑指 Offer 42. 连续子数组的最大和
- The mysqlbinlog command uses
- The problem and possible causes of the robot's instantaneous return to the origin of the world coordinate during rviz simulation
- 电脑清理,删除的系统文件
- hutool优雅解析URL链接并获取参数
- @Jsonbackreference and @jsonmanagedreference (solve infinite recursion caused by bidirectional references in objects)
- What is the role of automated testing frameworks? Shanghai professional third-party software testing company Amway
- Problems in loading and saving pytorch trained models
猜你喜欢

After PCD is converted to ply, it cannot be opened in meshlab, prompting error details: ignored EOF

Trying to use is on a network resource that is unavailable

Process of obtaining the electronic version of academic qualifications of xuexin.com

Fairguard game reinforcement: under the upsurge of game going to sea, game security is facing new challenges

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

项目连接数据库遇到的问题及解决

Image,cv2读取图片的numpy数组的转换和尺寸resize变化

Navicat premium create MySQL create stored procedure

TCP/IP协议

目标检测——Pytorch 利用mobilenet系列(v1,v2,v3)搭建yolov4目标检测平台
随机推荐
优秀的软件测试人员,都具备这些能力
查看局域网中电脑设备
What are the common processes of software stress testing? Professional software test reports issued by companies to share
LeetCode:387. 字符串中的第一个唯一字符
[Hacker News Weekly] data visualization artifact; Top 10 Web hacker technologies; Postman supports grpc
Tdengine biweekly selection of community issues | phase III
JVM quick start
Process of obtaining the electronic version of academic qualifications of xuexin.com
Detailed explanation of dynamic planning
Computer graduation design PHP Zhiduo online learning platform
LeetCode:214. 最短回文串
egg. JS directory structure
超高效!Swagger-Yapi的秘密
Alibaba cloud server mining virus solution (practiced)
深度剖析C语言指针
软件卸载时遇到trying to use is on a network resource that is unavailable
sublime text中conda环境中plt.show无法弹出显示图片的问题
SAP ui5 date type sap ui. model. type. Analysis of the parsing format of date
Niuke winter vacation training 6 maze 2
TP-LINK enterprise router PPTP configuration