Advertisement
nikunjsoni

221

Jun 9th, 2021
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.58 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int maximalSquare(vector<vector<char>>& mat) {
  4.         int rows = mat.size(), cols = mat[0].size();
  5.         int len = 0, dp[rows][cols];
  6.        
  7.         for(int i=0; i<rows; i++){
  8.             for(int j=0; j<cols; j++){
  9.                 dp[i][j] = (mat[i][j] == '1') ? 1 : 0;
  10.                 if(dp[i][j] && i && j){
  11.                     int sz = min({dp[i-1][j], dp[i][j-1], dp[i-1][j-1]});
  12.                     dp[i][j] = sz+1;
  13.                 }
  14.                 len = max(len, dp[i][j]);
  15.             }
  16.         }
  17.        
  18.         return len*len;
  19.     }
  20. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement