当前位置:网站首页>【动态规划】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
边栏推荐
- AUTOSAR from introduction to mastery 100 lectures (81) - FIM of AUTOSAR Foundation
- Nodejs + websocket cycle small case
- melt reshape decast 长数据短数据 长短转化 数据清洗 行列转化
- (personal) sorting out system vulnerabilities after recent project development
- 100 GIS practical application cases (52) - how to keep the number of rows and columns consistent and aligned when cutting grids with grids in ArcGIS?
- pyqt5 将opencv图片存入内置SQLlite数据库,并查询
- V-model binding value in El select, data echo only displays value, not label
- After the data of El table is updated, the data in the page is not updated this$ Forceupdate() has no effect
- HQL statement tuning
- Mui + hbuilder + h5api simulate pop-up payment style
猜你喜欢

拥抱机器视觉新蓝海,冀为好望开启数字经济发展新“冀”遇

Embrace the new blue ocean of machine vision and hope to open a new "Ji" encounter for the development of digital economy

PC starts multiple wechat at one time

Importerror after tensorflow installation: DLL load failed: the specified module cannot be found, and the domestic installation is slow

100 GIS practical application cases (51) - a method for calculating the hourly spatial average of NC files according to the specified range in ArcGIS

Recovering data with MySQL binlog

nodeJs + websocket 循环小案例

Learning notes of AMBA protocol

V-model binding value in El select, data echo only displays value, not label

Custom nail robot alarm
随机推荐
Wonderful review | the sixth issue of "source" - open source economy and industrial investment
AUTOSAR from introduction to mastery 100 lectures (81) - FIM of AUTOSAR Foundation
Record Alibaba cloud server mining program processing
Golang realizes regular matching: the password contains at least one digit, letter and special character, and the length is 8-16
Remote access to raspberry pie at home (Part 1)
Utils of various date conversion
7_Addmodule和基因加和法add 得到的细胞类型打分在空间上空转对比
Navicat远程连接数据库 出现 1130- Host xxx is not allowed to connect to this MySQL server错误
「玩转Lighthouse」轻量应用服务器自建DNS解析服务器
These vscode extensions are my favorite
Mui wechat payment pit
Golang implements a five insurance and one gold calculator with web interface
mui 关闭其他页面,只保留首页面
将opencv 图片转换为字节的方式
Custom nail robot alarm
100 GIS practical application cases (53) - making three-dimensional image map as the base map of urban spatial pattern analysis
The filter() traverses the array, which is extremely friendly
hbuilderx + uniapp 打包ipa提交App store踩坑记
Van uploader upload picture implementation process, using native input to upload pictures
MySQL supports IP access