forked from Masked-coder11/gfg-POTD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14.03.2024.cpp
50 lines (48 loc) · 1.27 KB
/
14.03.2024.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class Solution {
public:
int largestSubsquare(int n, vector<vector<char>> a) {
// code here
// we will be making by rows and by cols matrices
int by_rows[n][n];
int by_cols[n][n];
for(int i=0;i<n;i++){
int row=0;
for(int j=0;j<n;j++){
if(a[i][j]=='X'){
row++;
}
else{
row=0;
}
by_rows[i][j]=row;
}
}
for(int i=0;i<n;i++){
int col=0;
for(int j=0;j<n;j++){
if(a[j][i]=='X'){
col++;
}
else{
col=0;
}
by_cols[j][i]=col;
}
}
int res=0;
for(int i=n-1;i>=0;i--){
for(int j=n-1;j>=0;j--){
int side=min(by_rows[i][j], by_cols[i][j]);
while(side>res){
if(by_rows[i-side+1][j]>=side && by_cols[i][j-side+1]>=side){
res=side;
}
else{
side--;
}
}
}
}
return res;
}
};