-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path17.cpp
More file actions
executable file
·44 lines (41 loc) · 1.51 KB
/
17.cpp
File metadata and controls
executable file
·44 lines (41 loc) · 1.51 KB
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
class Solution {
public:
int maximalSquare(vector<vector<char> >& matrix) {
if (matrix.size() == 0) return 0;
int res = 0;
vector<vector<int>> answer;
for (int i = 0;i<matrix.size();i++){
std::vector<int> mid;
mid.clear();
for (int j = 0;j<matrix[i].size();j++)
if (matrix[i][j] == '1'){
mid.push_back(1);
res = 1;
} else
mid.push_back(0);
answer.push_back(mid);
}
for (int i = 1;i<answer.size();i++)
for (int j =1;j<answer[i].size();j++)
if (matrix[i][j] == '1'){
int lastrow = j - answer[i - 1][j - 1] - 1;
int lastcolumn = i - answer[i - 1][j - 1] - 1;
for (int k = j - 1; k >= j - answer[i-1][j-1]; k--)
if (matrix[i][k] == '0') {
lastrow = k;
break;
}
lastrow = j - lastrow;
for (int k = i - 1; k >= i - answer[i - 1][j - 1]; k--)
if (matrix[k][j] == '0') {
lastcolumn = k;
break;
}
lastcolumn = i - lastcolumn;
int last = min(lastcolumn,lastrow);
answer[i][j] = max(last,answer[i][j]);
if (answer[i][j] > res) res = answer[i][j];
}
return res*res;
}
};