[LintCode] Maximal Square II

Given a 2D binary matrix filled with 0's and 1's, find the largest square which diagonal is all 1 and others is 0.

Only consider the main diagonal situation.
Example

For example, given the following matrix:

1 0 1 0 0
1 0 0 1 0
1 1 0 0 1
1 0 0 1 0

Return 9

 

Similiarly with Maximal Square, we can check each entry of 1 from scratch and get the max length of a square whose diagonal is all 1 and others are 0.

Again, this straightforward solution suffers the same NOT using previous check results to expediate our algorithm's runtime.

 

So we jump directly into the dynamic programming solution. 

State: dp[i][j]: the max length of a square with only diagonal 1s whose bottom right corner is matrix[i][j]. 

Function: dp[i][j] = 0, if matrix[i][j] == 0;

     dp[i][j] = 1 + min {leftZeros[i][j], upZeros[i][j], dp[i - 1][j - 1]}, if matrix[i][j] == 1;

Similarly with Maxmial Square, when matrix[i][j] is 1, we also need to check its left, top, top left side.

leftZeros[i][j] is the max number of consecutive 0s to the left of matrix[i][j];

upZeros[i][j] is the max number of consecutive 0s to the top of matrix[i][j];

These two along with dp[i - 1][j - 1] decides the max length of a sqaure with only diagonal 1s whose bottom right corner is matrix[i][j]. 

 

 1 public class Solution {
 2     public int maxSquare2(int[][] matrix) {
 3         if(matrix == null || matrix.length == 0 || matrix[0].length == 0){
 4             return 0;
 5         }
 6         int n = matrix.length; int m = matrix[0].length;
 7         int[][] leftZeros = new int[n][m];
 8         int[][] upZeros = new int[n][m];
 9         for(int i = 0; i < n; i++){
10             leftZeros[i][0] = 0;
11         }
12         for(int j = 0; j < m; j++){
13             upZeros[0][j] = 0;
14         }
15         for(int i = 0; i < n; i++){
16             for(int j = 1; j < m; j++){
17                 if(matrix[i][j - 1] == 0){
18                     leftZeros[i][j] = leftZeros[i][j - 1] + 1;
19                 }    
20                 else{
21                     leftZeros[i][j] = 0;
22                 }
23             }
24         }
25         for(int i = 1; i < n; i ++){
26             for(int j = 0; j < m; j++){
27                 if(matrix[i - 1][j] == 0){
28                     upZeros[i][j] = upZeros[i - 1][j] + 1;
29                 }
30                 else{
31                     upZeros[i][j] = 0;
32                 }                
33             }
34         }
35         int[][] dp = new int[n][m];
36         for(int i = 0; i < n; i++){
37             dp[i][0] = matrix[i][0];    
38         }
39         for(int j = 0; j < m; j++){
40             dp[0][j] = matrix[0][j];
41         }
42         for(int i = 1; i < n; i++){
43             for(int j = 1; j < m; j++){
44                 if(matrix[i][j] == 0){
45                     dp[i][j] = 0;
46                 }
47                 else{
48                     dp[i][j] = Math.min(Math.min(leftZeros[i][j], upZeros[i][j]), dp[i - 1][j - 1]) + 1;
49                 }
50             }
51         }
52         int max = 0;
53         for(int i = 0; i < n; i++){
54             for(int j = 0; j < m; j++){
55                 max = Math.max(max, dp[i][j]);    
56             }
57         }
58         return max * max;
59     }
60 }

 

 

Related Problems

Maximal Square

Maximum Subsquare surrounded by 'X'

转载于:https://www.cnblogs.com/lz87/p/7393780.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值