当前位置:网站首页>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;
};
边栏推荐
- Philosophical enlightenment from single point to distributed
- Mongodb installation and basic operation
- 电脑清理,删除的系统文件
- C language double pointer -- classic question type
- What is the role of automated testing frameworks? Shanghai professional third-party software testing company Amway
- 自动化测试框架有什么作用?上海专业第三方软件测试公司安利
- The network model established by torch is displayed by torch viz
- 【嵌入式】Cortex M4F DSP库
- Deep analysis of C language pointer
- 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
猜你喜欢
Screenshot in win10 system, win+prtsc save location
Simple use of promise in uniapp
Nacos 的安装与服务的注册
生成器参数传入参数
Mongodb installation and basic operation
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
swagger设置字段required必填
opencv+dlib实现给蒙娜丽莎“配”眼镜
查看局域网中电脑设备
Image,cv2读取图片的numpy数组的转换和尺寸resize变化
随机推荐
[NVIDIA development board] FAQ (updated from time to time)
Current situation and trend of character animation
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
UML diagram memory skills
hutool优雅解析URL链接并获取参数
marathon-envs项目环境配置(强化学习模仿参考动作)
Compétences en mémoire des graphiques UML
Target detection - pytorch uses mobilenet series (V1, V2, V3) to build yolov4 target detection platform
LeetCode:387. 字符串中的第一个唯一字符
Generator parameters incoming parameters
超高效!Swagger-Yapi的秘密
gcc动态库fPIC和fpic编译选项差异介绍
LeetCode:836. 矩形重叠
深度剖析C语言指针
Charging interface docking tutorial of enterprise and micro service provider platform
TDengine 社区问题双周精选 | 第三期
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
个人电脑好用必备软件(使用过)
@Jsonbackreference and @jsonmanagedreference (solve infinite recursion caused by bidirectional references in objects)
电脑F1-F12用途