nikunjsoni

1277

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