-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path112.Maximal Square.cpp
More file actions
27 lines (24 loc) · 876 Bytes
/
Copy path112.Maximal Square.cpp
File metadata and controls
27 lines (24 loc) · 876 Bytes
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
class Solution {
public:
int maximalSquare(vector<vector<char> >& matrix) {
vector<vector<int> > res(matrix.size(),vector<int>(matrix.at(0).size(),0));
int min_area=0;
for(int i=0; i<res.size(); i++)
res[i][0]=matrix[i][0]-'0';
for(int j=0; j<res.at(0).size(); j++)
res[0][j]=matrix[0][j]-'0';
for(int i=1; i<res.size(); i++) {
for(int j=1; j<res.at(0).size(); j++) {
if(matrix[i][j]-'0'!=0) {
res[i][j]=1+min({res[i-1][j],res[i][j-1],res[i-1][j-1]});
}
}
}
for(int i=0; i<res.size(); i++) {
for(int j=0; j<res.at(0).size(); j++) {
min_area=max(min_area,res[i][j]);
}
}
return min_area*min_area;
}
};