LeetCode Maximal Square DP
来源:互联网 发布:淘宝摄影兼职 编辑:程序博客网 时间:2024/05/20 18:18
思路:
要想从面积为1扩展到面积为2,需要该点的左方,上方和左上方都为1才行,即下列任一种情况都不行:
0 00 **1**
1 00 **1**
0 01 **1**
0 10 **1**
…
必须满足:
1 11 **1**
有了这点规律,我们就以右下角为基础建立我们的递推方程和递推矩阵: dp[i][j] = min(dp[i-1][j-1], dp[i][j-1], dp[i-1][j]) + 1
,其中,dp[i][j]
表示以当前点为右下角的正方形的最大边长。
时间复杂度O(N^2),空间复杂度O(N^2)。
java code:
public class Solution { public int maximalSquare(char[][] matrix) { if(matrix == null || matrix.length == 0) return 0; int n = matrix.length;//row int m = matrix[0].length;//col int dp[][] = new int[n][m]; int max = 0; //init for(int i = 0; i < n; ++i) { if(matrix[i][0] == '1') { dp[i][0] = 1; max = 1; } } for(int i = 0; i < m; ++i) { if(matrix[0][i] == '1') { dp[0][i] = 1; max = 1; } } //dp construct for(int i = 1; i < n; ++i) { for(int j = 1; j < m; ++j) { if(matrix[i][j] == '0') { dp[i][j] = 0; }else { dp[i][j] = Math.min(Math.min(dp[i-1][j-1], dp[i-1][j]), dp[i][j-1]) + 1; max = Math.max(max, dp[i][j]); } } } return max * max; }}
0 0
- LeetCode Maximal Square DP
- leetcode----DP----Maximal Square
- leetcode -- Maximal Square -- DP重点
- [Leetcode] #221 Maximal Square (DP)
- [LeetCode] Maximal Square(!!!!DP优化)
- Leetcode 221 - Maximal Square(dp)
- 【LeetCode】Maximal Rectangle && Maximal Square
- Leetcode:Maximal Square & Maximal Rectangle
- leetcode013-Maximal Square(dp)
- [leetcode] Maximal Square
- Maximal Square Leetcode Java
- [leetcode] Maximal Square
- leetcode 221: Maximal Square
- LeetCode Maximal Square
- Maximal Square - LeetCode 221
- #leetcode#Maximal Square
- LeetCode Maximal Square
- [LeetCode] Maximal Square
- ]巧妙洗头,白发变黑发
- Android TouchDelegate
- expect spawn、linux expect 用法
- C语言结构体用法详解
- MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作
- LeetCode Maximal Square DP
- java类初始化顺序
- SOM 工具箱简易入门
- HTTP中在JAVAEE中最简单的应用
- java中queue的使用
- struts2 action 页面跳转,有两个跳转类型是亲试过
- Elasticsearch - 搜索引擎入门
- 数据持久化
- 1553B命令字与消息传输