Advertisement
HXXXXJ

221. Maximal Square

Sep 12th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.71 KB | None | 0 0
  1.     public int maximalSquare(char[][] matrix) {
  2.         int n = matrix.length;
  3.         if(n == 0) return 0;
  4.         int m = matrix[0].length;
  5.        
  6.         int[][] dp = new int[2][m+1];
  7.         int max = 0;
  8.         int pre = 0;
  9.         int cur = 1;
  10.         for(int i = 1 ; i <= n; ++i){
  11.             for(int j = 1 ; j <= m ; ++j){
  12.                 if(matrix[i-1][j-1] == '0'){
  13.                     dp[cur][j] = 0;
  14.                     continue;
  15.                 }
  16.                 dp[cur][j] = Math.min(Math.min(dp[cur][j-1],dp[pre][j]), dp[pre][j-1]) +1;
  17.                 max = Math.max(dp[cur][j], max);
  18.  
  19.             }
  20.             pre = cur;
  21.             cur = (cur +1)%2;
  22.         }
  23.         return max*max;
  24.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement