当前位置:网站首页>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;
};
边栏推荐
- Guangzhou will promote the construction of a child friendly city, and will explore the establishment of a safe area 200 meters around the school
- TCP/IP协议
- 【嵌入式】使用JLINK RTT打印log
- 同一局域网的手机和电脑相互访问,IIS设置
- ESP8266-RTOS物联网开发
- R language ggplot2 visualization: place the title of the visualization image in the upper left corner of the image (customize Title position in top left of ggplot2 graph)
- UnsupportedOperationException异常
- pytorch查看张量占用内存大小
- swagger设置字段required必填
- LeetCode:394. 字符串解码
猜你喜欢

个人电脑好用必备软件(使用过)

Cesium draw points, lines, and faces

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

Deep anatomy of C language -- C language keywords

【剑指offer】序列化二叉树

Image, CV2 read the conversion and size resize change of numpy array of pictures

UML圖記憶技巧
![[today in history] February 13: the father of transistors was born The 20th anniversary of net; Agile software development manifesto was born](/img/70/d275009134fcbf9ae984c0f278659e.jpg)
[today in history] February 13: the father of transistors was born The 20th anniversary of net; Agile software development manifesto was born

Screenshot in win10 system, win+prtsc save location

深度剖析C语言数据在内存中的存储
随机推荐
[embedded] cortex m4f DSP Library
有效提高软件产品质量,就找第三方软件测评机构
Simple use of promise in uniapp
Leetcode: Sword finger offer 42 Maximum sum of continuous subarrays
生成器参数传入参数
LeetCode:162. 寻找峰值
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
How to effectively conduct automated testing?
Sublime text using ctrl+b to run another program without closing other runs
MySQL uninstallation and installation methods
PC easy to use essential software (used)
Bitwise logical operator
Roguelike game into crack the hardest hit areas, how to break the bureau?
【剑指offer】序列化二叉树
How to conduct interface test? What are the precautions? Nanny level interpretation
Export IEEE document format using latex
C語言雙指針——經典題型
Light of domestic games destroyed by cracking
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语言数据在内存中的存储