[LeetCode]--221. Maximal Square
2016-08-06 18:46
381 查看
public class Solution { public int maximalSquare(char[][] a) { if (a == null || a.length == 0 || a[0].length ==0) return 0; int max = 0, m = a.length, n = a[0].length; // dp[i,j] represents the length of the square whose lower-right is //located at (i, j) , dp[i,j] = min{dp[i - 1][j - 1], dp[i - 1][j], dp[i][j - 1]}, if a[i][j] = 1, // add 1 int[][] dp = new int[m + 1][n + 1]; for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (a[i - 1][j - 1] == '1') { dp[i][j] = Math.min(dp[i - 1][j - 1], Math.min(dp[i - 1][j], dp[i][j - 1])) + 1; max = Math.max(max, dp[i][j]); //Think carefully about it! } } } return (int)(Math.pow(max, 2)); } }
It’s Dynamic Programming!
Reference: https://discuss.leetcode.com/topic/18482/accepted-clean-java-dp-solution
相关文章推荐
- [LeetCode]221. Maximal Square & 85. Maximal Rectangle
- [leetcode]221. Maximal Square
- leetcode 221. Maximal Square
- Leetcode 221. Maximal Square
- leetcode 221. Maximal Square 最大正方形面积 + 动态规划DP实现
- LeetCode 221. Maximal Square
- Leetcode 221. Maximal Square (Medium) (cpp)
- leetcode 221. Maximal Square
- LeetCode *** 221. Maximal Square
- leetcode 221. Maximal Square
- [LeetCode]221. Maximal Square
- 221. Maximal Square LeetCode
- 【LeetCode】221. Maximal Square
- [leetcode]221. Maximal Square
- LeetCode 221. Maximal Square
- Leetcode 221. Maximal Square
- LeetCode 85. Maximal Rectangle&221. Maximal Square--动态规划
- leetcode 221. Maximal Square & 85. Maximal Rectangle
- **Leetcode 221. Maximal Square | dp
- Leetcode 221. Maximal Square