-
Notifications
You must be signed in to change notification settings - Fork 3
/
764.cpp
56 lines (52 loc) · 1.85 KB
/
764.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
class Solution {
public:
int orderOfLargestPlusSign(int n, vector<vector<int>>& mines) {
vector<vector<bool>> matrix(n, vector<bool>(n, true));
for (auto& mine : mines) {
matrix[mine[0]][mine[1]] = false;
}
vector<vector<int>> left(n, vector<int>(n, 0));
vector<vector<int>> right(n, vector<int>(n, 0));
vector<vector<int>> up(n, vector<int>(n, 0));
vector<vector<int>> down(n, vector<int>(n, 0));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (matrix[i][j] == false) right[i][j] = 0;
else {
if (j > 0) right[i][j] = right[i][j - 1] + 1;
else right[i][j] = 1;
}
}
for (int j = n - 1; j >= 0; --j) {
if (matrix[i][j] == false) left[i][j] = 0;
else {
if (j < n - 1) left[i][j] = left[i][j + 1] + 1;
else left[i][j] = 1;
}
}
}
for (int j = 0; j < n; ++j) {
for (int i = 0; i < n; ++i) {
if (matrix[i][j] == false) down[i][j] = 0;
else {
if (i > 0) down[i][j] = down[i - 1][j] + 1;
else down[i][j] = 1;
}
}
for (int i = n - 1; i >= 0; --i) {
if (matrix[i][j] == false) up[i][j] = 0;
else {
if (i < n - 1) up[i][j] = up[i + 1][j] + 1;
else up[i][j] = 1;
}
}
}
int res = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
res = max(res, min({up[i][j], down[i][j], left[i][j], right[i][j]}));
}
}
return res;
}
};