当前位置:网站首页>【动态规划】221. 最大正方形
【动态规划】221. 最大正方形
2022-04-23 13:07:00 【不爱研究的研究僧】
题目:
在一个由 '0'
和 '1'
组成的二维矩阵内,找到只包含 '1'
的最大正方形,并返回其面积。
题解:
dp 具体定义:dp[i + 1][j + 1] 表示 「以第 i 行、第 j 列为右下角的正方形的最大边长」
为何不是 dp[i][j]
任何一个正方形,我们都「依赖」当前格 左、上、左上三个方格的情况,但第一行的上层已经没有格子,第一列左边已经没有格子,需要做特殊 if 判断来处理,为了代码简洁,我们 假设补充 了多一行全 '0'、多一列全 '0'
此时 dp 数组的大小也明确为 new dp[height + 1][width + 1]
初始值就是将第一列 dp[row][0] 、第一行 dp[0][col] 都赋为 0,相当于已经计算了所有的第一行、第一列的 dp 值
题目要求面积。根据 「面积 = 边长 x 边长」可知,我们只需求出 最大边长 即可
定义 maxSide 表示最长边长,每次得出一个 dp,就 maxSide = max(maxSide, dp);
最终返回 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]; // 相当于已经预处理新增第一行、第一列均为0
for (int i = 0 ; i < h; i++) { //遍历matrix
for (int j = 0; j < w; j++) {
if (matrix[i][j] == '1') {
//取左边、右边、左上最小长度加上当前单元格长度1
//注意左边、右边、左上是以[i + 1][j + 1]为基准,而不是[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;
}
}
参考:力扣
版权声明
本文为[不爱研究的研究僧]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_43955488/article/details/124361049
边栏推荐
- async void 導致程序崩潰
- (1) Openjuterpyrab comparison scheme
- Hanlp word splitter (via spark)
- 4.22 study record (you only did water problems in one day, didn't you)
- MySQL —— 16、索引的数据结构
- "Play with Lighthouse" lightweight application server self built DNS resolution server
- MySQL -- 16. Data structure of index
- async void 导致程序崩溃
- Teach you to quickly develop a werewolf killing wechat applet (with source code)
- Customize the shortcut options in El date picker, and dynamically set the disabled date
猜你喜欢
Design of STM32 multi-channel temperature measurement wireless transmission alarm system (industrial timing temperature measurement / engine room temperature timing detection, etc.)
Nodejs + Mysql realize simple registration function (small demo)
Design of body fat detection system based on 51 single chip microcomputer (51 + OLED + hx711 + US100)
jmeter操作redis
MySQL —— 16、索引的数据结构
Record some NPM related problems (messy records)
Packet capturing and sorting -- TCP protocol [8]
STM32 is connected to the motor drive, the DuPont line supplies power, and then the back burning problem
MySQL -- 16. Data structure of index
Remote access to raspberry pie at home (Part 1)
随机推荐
Subscribe to Alibaba demo send business messages
Importerror after tensorflow installation: DLL load failed: the specified module cannot be found, and the domestic installation is slow
7_ The cell type scores obtained by addmodule and gene addition method are compared in space
这几种 VSCode 扩展是我最喜欢的
拥抱机器视觉新蓝海,冀为好望开启数字经济发展新“冀”遇
Read the data in Presto through sparksql and save it to Clickhouse
The El table horizontal scroll bar is fixed at the bottom of the visual window
Teach you to quickly develop a werewolf killing wechat applet (with source code)
Introducing vant components on demand
Record the problems encountered in using v-print
mysql8安装
Design and manufacture of 51 single chip microcomputer solar charging treasure with low voltage alarm (complete code data)
1130 - host XXX is not allowed to connect to this MySQL server error in Navicat remote connection database
Async void provoque l'écrasement du programme
Melt reshape decast long data short data length conversion data cleaning row column conversion
PC starts multiple wechat at one time
Get the punch in record of nailing attendance machine
Timing role in the project
Golang implements MD5, sha256 and bcrypt encryption
Translation of attention in natural language processing