当前位置:网站首页>[dynamic programming] 221 Largest Square
[dynamic programming] 221 Largest Square
2022-04-23 13:11:00 【A research monk who doesn't like research】
subject :
In a '0'
and '1'
In the two-dimensional matrix , Found contains only '1'
The largest square of , And return its area .
Answer key :
dp Specific definition :dp[i + 1][j + 1] Express 「 By the end of i That's ok 、 The first j The maximum side length of the square listed as the lower right corner 」
Why not dp[i][j]
Any square , We all 「 rely on 」 Current grid Left 、 On 、 The situation of the upper left three squares , But the upper layer of the first row has no grid , There is no grid on the left of the first column , Need to do special if Deal with it by judgment , For code simplicity , We Suppose to add One more line '0'、 One more train, all '0'
here dp The size of the array is also specified as new dp[height + 1][width + 1]
The initial value is to put the first column dp[row][0] 、 first line dp[0][col] All Fu Wei 0, The first line is equivalent to all calculations 、 In the first column dp value
The title requires an area of . according to 「 area = Side length x Side length 」 You know , We just need to figure out Maximum side length that will do
Definition maxSide Indicates the longest side length , One at a time dp, Just maxSide = max(maxSide, dp);
Eventually return return maxSide * maxSide;
class Solution {
public int maximalSquare(char[][] matrix) {
if (matrix == null || matrix.length < 1 || matrix[0].length < 0) {
return 0;
}
int h = matrix.length;
int w = matrix[0].length;
int maxLen = 0;
int[][] dp = new int[h + 1][w + 1]; // It is equivalent to adding the first line after preprocessing 、 The first column is 0
for (int i = 0 ; i < h; i++) { // Traverse matrix
for (int j = 0; j < w; j++) {
if (matrix[i][j] == '1') {
// Take the left 、 On the right 、 The minimum length at the top left plus the current cell length 1
// Pay attention to the left 、 On the right 、 Top left is [i + 1][j + 1] Benchmarking , instead of [i][j]
dp[i + 1][j + 1] = Math.min(Math.min(dp[i + 1][j], dp[i][j + 1]), dp[i][j]) + 1;
}
maxLen = Math.max(maxLen, dp[i + 1][j + 1]);
}
}
return maxLen * maxLen;
}
}
Reference resources : Power button
版权声明
本文为[A research monk who doesn't like research]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231307030210.html
边栏推荐
- Hanlp word splitter (via spark)
- 基于uniapp异步封装接口请求简介
- mysql 基本语句查询
- XML
- 4.22 study record (you only did water problems in one day, didn't you)
- STM32 tracking based on open MV
- Free and open source charging pile Internet of things cloud platform
- Mui wechat payment pit
- LeetCode_DFS_中等_695.岛屿的最大面积
- Pyqt5 store opencv pictures into the built-in sqllite database and query
猜你喜欢
普通大学生如何拿到大厂offer?敖丙教你一招致胜!
【动态规划】221. 最大正方形
MySQL5.5安装教程
Use Proteus to simulate STM32 ultrasonic srf04 ranging! Code+Proteus
内核错误: No rule to make target ‘debian/canonical-certs.pem‘, needed by ‘certs/x509_certificate_list‘
超40W奖金池等你来战!第二届“长沙银行杯”腾讯云启创新大赛火热来袭!
C语言之字符串与字符数组的区别
Important knowledge of transport layer (interview, retest, final)
Design of body fat detection system based on 51 single chip microcomputer (51 + OLED + hx711 + US100)
ESP32 VHCI架构传统蓝牙设置scan mode,让设备能被搜索到
随机推荐
Armv8m (cortex M33) MPU actual combat
Translation of attention in natural language processing
Nodejs + websocket cycle small case
Summary of JVM knowledge points - continuously updated
Jupiter notebook installation
HQL statement tuning
(1) Openjuterpyrab comparison scheme
Free and open source intelligent charging pile SaaS cloud platform of Internet of things
超40W奖金池等你来战!第二届“长沙银行杯”腾讯云启创新大赛火热来袭!
mui picker和下拉刷新冲突问题
The use of dcast and melt in R language is simple and easy to understand
Common interview questions and detailed analysis of the latest Android developers in 2020
2020年最新字节跳动Android开发者常见面试题及详细解析
SQL exercise question 1
Introduction to metalama 4 Use fabric to manipulate items or namespaces
1130 - host XXX is not allowed to connect to this MySQL server error in Navicat remote connection database
Temperature and humidity monitoring + timing alarm system based on 51 single chip microcomputer (C51 source code)
AUTOSAR from introduction to mastery 100 lectures (81) - FIM of AUTOSAR Foundation
Design of body fat detection system based on 51 single chip microcomputer (51 + OLED + hx711 + US100)
Use Proteus to simulate STM32 ultrasonic srf04 ranging! Code+Proteus