Daily Challenge | Day 20
\n\n# Code\ncpp []\nclass Solution {\npublic:\n    int rows, cols;\n    int largestSquare(int x, int y, vector<vector<int>>& grid, vector<vector<int>>& hor, vec
\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int rows, cols;\n    int largestSquare(int x, int y, vector<vector<int>>& grid, vector<vector<int>>& hor, vector<vector<int>>& ver) {\n        int len = min(hor[x][y], ver[x][y]) - 1;// min dist b/w horiz & vert. line containing ones \n        for(int i = len; i >= 0; i--) { // shrink if unable to make\n            if(i+x < rows && i+y < cols && ver[x][y+i] > i && hor[i+x][y] > i)\n                return (i+1) * (i+1);\n        }\n\n        return 0;     \n    }\n\n    int largest1BorderedSquare(vector<vector<int>>& grid) {\n        rows = grid.size(), cols = grid[0].size();\n\n        vector<vector<int>> hor( rows , vector<int> (cols));  \n        vector<vector<int>> ver( rows , vector<int> (cols));  \n\n        // count no. of 1\'s from right to  i,j (<----- this dir. in grid)\n        for(int i = 0; i < rows; i++) {\n            int cnt = 0;\n            for(int j = cols - 1; j >= 0; j--) {\n                if(grid[i][j]) ++cnt;\n                else cnt = 0;\n                hor[i][j] = cnt;\n            }\n        }\n\n        for(int i = 0; i < cols; i++) {\n            int cnt = 0;\n            for(int j = rows-1; j >= 0; j--) {\n                if(grid[j][i]) ++cnt;\n                else cnt = 0;\n                ver[j][i] = cnt;\n            }\n        }\n\n        int mx = 0;\n        for(int i = 0; i < rows; i++) {\n            for(int j = 0; j < cols; j++) {\n                int size = largestSquare(i, j, grid, hor, ver);\n                mx = max(mx, size);\n            }\n        }\n\n        return mx;\n    }\n};\n\n/*\n[ 1,1,1 ],\n[ 1,0,1 ],\n[ 1,1,1 ],\n*/\n```
['C++']