Advertisement
Guest User

Grokking 202

a guest
Dec 22nd, 2021
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.65 KB | None | 0 0
  1. class Solution {
  2.   public int maximalSquare(char[][] matrix) {
  3.     int rows = matrix.length;
  4.     int cols = matrix[0].length;
  5.    
  6.     int[][] dp = new int[rows + 1][cols + 1];
  7.    
  8.     int maxsqlen = 0;
  9.    
  10.     for (int row = 1; row <= rows; row++) {
  11.       for (int col = 1; col <= cols; col++) {
  12.        
  13.         if (matrix[row - 1][col - 1] == '1') {
  14.           dp[row][col] = 1 + Math.min(
  15.             dp[row - 1][col - 1],
  16.             Math.min(dp[row - 1][col], dp[row][col - 1])
  17.           );
  18.            
  19.           maxsqlen = Math.max(maxsqlen, dp[row][col]);
  20.         }
  21.        
  22.       }
  23.     }
  24.    
  25.     return maxsqlen * maxsqlen;
  26.   }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement