forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid-illumination.cpp
63 lines (59 loc) · 1.98 KB
/
grid-illumination.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
// Time: O(l + q)
// Space: O(l)
class Solution {
public:
template <typename T>
struct PairHash {
size_t operator()(const pair<T, T>& p) const {
size_t seed = 0;
seed ^= std::hash<T>{}(p.first) + 0x9e3779b9 + (seed<<6) + (seed>>2);
seed ^= std::hash<T>{}(p.second) + 0x9e3779b9 + (seed<<6) + (seed>>2);
return seed;
}
};
public:
vector<int> gridIllumination(int N, vector<vector<int>>& lamps, vector<vector<int>>& queries) {
static const vector<pair<int, int>> directions =
{{0, -1}, {0, 1}, {-1, 0}, {1, 0},
{-1, -1}, {1, -1}, {-1, 1}, {1, 1}};
unordered_set<pair<int, int>, PairHash<int>> lookup;
unordered_map<int, int> row;
unordered_map<int, int> col;
unordered_map<int, int> diag;
unordered_map<int, int> anti;
for (const auto& lamp : lamps) {
int r, c;
tie(r, c) = make_pair(lamp[0], lamp[1]);
lookup.emplace(r, c);
++row[r];
++col[c];
++diag[r - c];
++anti[r + c];
}
vector<int> result;
for (const auto& query : queries) {
int r, c;
tie(r, c) = make_pair(query[0], query[1]);
if (row[r] || col[c] ||
diag[r - c] || anti[r + c]) {
result.emplace_back(1);
} else {
result.emplace_back(0);
}
for (const auto& d : directions) {
int nr, nc;
tie(nr, nc) = make_pair(r + d.first, c + d.second);
if (!(0 <= nr && nr < N && 0 <= nc && nc < N &&
lookup.count(make_pair(nr, nc)))) {
continue;
}
lookup.erase(make_pair(nr, nc));
--row[nr];
--col[nc];
--diag[nr - nc];
--anti[nr + nc];
}
}
return result;
}
};