当前位置:网站首页>221. Largest Square
221. Largest Square
2022-07-31 01:52:00 【ZNineSun】
打卡!!!每日一题
今天继续给大家分享一道动态规划类型的题目,However, the state transition equation of this topic is more difficult to understand,I will give you a detailed analysis
题目描述:
在一个由 ‘0’ 和 ‘1’ 组成的二维矩阵内,找到只包含 ‘1’ 的最大正方形,并返回其面积.
题目示例:

本题思路很简单,直接暴力解决,两个for循环遍历,Traverse all the way to the bottom right node,Then find an area maximum,Because the time complexity is too high,Also sorry for our IQ(PS:Just listen to my bragging,In the real written test or interview, you must use the fastest speed to solve it,只要能AC就是厉害),Next we discuss the solution of dynamic programming.
我们定义dp[i][j],表示以(i,j)为右下角的,且只包含 1 的正方形的边长最大值
通俗来讲,Like the corresponding square under the red area shown above,Of course the square might not be that big,I just drew a random one for everyone to understand the meaning of the lower right corner.
如果我们能计算出所有 dp(i,j) 的值,那么其中的最大值即为矩阵中只包含 1 的正方形的边长最大值,其平方即为最大正方形的面积.
那么如何计算 dp 中的每个元素值呢?对于每个位置 (i, j),检查在矩阵中该位置的值:
- 如果该位置的值是 0,则 dp(i, j) = 0,因为当前位置不可能在由 1 组成的正方形中;
- 如果该位置的值是 1,则 dp(i,j) 的值由其上方、左方和左上方的三个相邻位置的 dp 值决定.具体而言,当前位置的元素值等于三个相邻位置的元素中的最小值加 1,状态转移方程如下:

Let me help you understand why the minimum of these three is taken
我们假设A,B,C的形状如下:
说明EThe value in the area must be 0,否则的话CThe side length can be taken2了
如果Dthe side length ofA的边长+1,即D的边长=4,Then it consists of a large square,一定包含E区域,这就不合理了,Because the title requires that our squares must all be1
如果还是不理解,下面留言吧,实在不行,I have time to record a video to explain it to you.
If this is understood,代码就很简单了:
public int maximalSquare(char[][] matrix) {
int row = matrix.length;
int coloumn = matrix[0].length;
if (row <= 1 && coloumn <= 1) {
return matrix[0][0];
}
int[][] dp = new int[row][coloumn];
for (int r = 0; r < row; r++) {
for (int c = 0; c < coloumn; c++) {
if (matrix[r][c] == '0') {
dp[r][c] = 0;
} else {
if (r - 1 < 0 || c - 1 < 0) {
dp[r][c] = 1;
} else {
dp[r][c] = Math.min(Math.min(dp[r][c - 1], dp[r - 1][c]), dp[r - 1][c - 1]) + 1;
}
}
}
}
int max = dp[0][0];
for (int i = 0; i < row; i++) {
for (int j = 0; j < coloumn; j++) {
max = Math.max(dp[i][j], max);
}
}
return max * max;
}
边栏推荐
- 16、注册中心-consul
- Simple confession page
- Basic introduction to ShardingJDBC
- Observer mode (1)
- Software testing basic interface testing - getting started with Jmeter, you should pay attention to these things
- 打印任务排序 js od华为
- "Cloud native's master, master and vulgar skills" - 2022 National New College Entrance Examination Volume I Composition
- 基于FPGA的售货机
- Introduction and use of Drools WorkBench
- Force buckled brush the stairs (7/30)
猜你喜欢

VSCode插件:嵌套注释

Basic introduction to ShardingJDBC

MySQL的存储过程

两个有序数组间相加和的Topk问题

"Cloud native's master, master and vulgar skills" - 2022 National New College Entrance Examination Volume I Composition

pycharm cannot run after renaming (error: can't open file...No such file or directory)

CV-Model【3】:MobileNet v2

rpm安装postgresql12

Overview of prometheus monitoring

Problems that need to be solved by the tcp framework
随机推荐
[1153] The boundary range of between in mysql
What have I experienced to become a tester who is harder than development?
Charging effect simulation
内网渗透——提权
Crawler text data cleaning
MySQL的存储过程
Verify the integer input
力扣刷题之爬楼梯(7/30)
爬虫文本数据清洗
Jiuzhou Cloud was selected into the "Trusted Cloud's Latest Evaluation System and the List of Enterprises Passing the Evaluation in 2022"
uniapp uses 3rd party fonts
[WeChat applet] This article takes you to understand data binding, event binding, event parameter transfer, and data synchronization
Observer mode (1)
曼城推出可检测情绪的智能围巾,把球迷给整迷惑了
Brute Force/Adjacency Matrix Breadth First Directed Weighted Graph Undirected Weighted Graph
成为比开发硬气的测试人,我都经历了什么?
My first understanding of MySql, and the basic syntax of DDL and DML and DQL in sql statements
有没有可以做副业可以日入300元方法?
leetcode-1161: Maximum in-layer element sum
rpm安装postgresql12