-
Notifications
You must be signed in to change notification settings - Fork 0
/
200420-1.cpp
77 lines (75 loc) · 1.65 KB
/
200420-1.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// https://leetcode-cn.com/problems/maximal-square/
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix) {
int maxEdge = 0;
for (int i = 0; i < matrix.size(); ++i) {
for (int j = 0; j < matrix[i].size(); ++j) {
if (matrix[i][j] == '1') {
int e = getMaxEdge(matrix, i, j);
if (e > maxEdge) maxEdge = e;
}
}
}
return maxEdge * maxEdge;
}
private:
int getMaxEdge(const vector<vector<char>>& a, int i, int j) {
int edge = 1;
for (;; ++edge) {
if (i + edge >= a.size()) break;
if (j + edge >= a[i].size()) break;
bool ok = true;
for (int k = 0; k <= edge; ++k) {
if (a[i + edge][j + k] == '0') {
ok = false;
break;
}
if (a[i + k][j + edge] == '0') {
ok = false;
break;
}
}
if (!ok) break;
}
return edge;
}
};
int main()
{
Solution s;
{
vector<vector<char>> a = {
{'1', '0', '1', '0', '0'},
{'1', '0', '1', '1', '1'},
{'1', '1', '1', '1', '1'},
{'1', '0', '0', '1', '0'},
};
cout << s.maximalSquare(a) << endl; // answer: 4
}
{
vector<vector<char>> a = {
{'1', '1'},
{'1', '1'},
};
cout << s.maximalSquare(a) << endl; // answer: 4
}
{
vector<vector<char>> a = {
{'0','0','0','1','0','1','1','1'},
{'0','1','1','0','0','1','0','1'},
{'1','0','1','1','1','1','0','1'},
{'0','0','0','1','0','0','0','0'},
{'0','0','1','0','0','0','1','0'},
{'1','1','1','0','0','1','1','1'},
{'1','0','0','1','1','0','0','1'},
{'0','1','0','0','1','1','0','0'},
{'1','0','0','1','0','0','0','0'}
};
cout << s.maximalSquare(a) << endl; // answer: 4
}
return 0;
}