forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid-illumination.cpp
44 lines (42 loc) · 1.4 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
// Time: O(l + q)
// Space: O(l)
class Solution {
public:
vector<int> gridIllumination(int N, vector<vector<int>>& lamps, vector<vector<int>>& queries) {
unordered_set<int64_t> lookup;
unordered_map<int, int> row, col, diag, anti;
for (const auto& lamp : lamps) {
int r, c;
tie(r, c) = make_pair(lamp[0], lamp[1]);
if (!lookup.emplace(static_cast<int64_t>(r) * N + c).second) {
continue;
}
++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(0);
continue;
}
result.emplace_back(1);
for (int nr = max(r - 1, 0); nr <= min(r + 1, N - 1); ++nr) {
for (int nc = max(c - 1, 0); nc <= min(c + 1, N - 1); ++nc) {
if (!lookup.erase(static_cast<int64_t>(nr) * N + nc)) {
continue;
}
--row[nr];
--col[nc];
--diag[nr - nc];
--anti[nr + nc];
}
}
}
return result;
}
};