forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.ts
47 lines (47 loc) · 1.42 KB
/
Solution.ts
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
function gridIllumination(
n: number,
lamps: number[][],
queries: number[][],
): number[] {
const row = new Map<number, number>();
const col = new Map<number, number>();
const diag1 = new Map<number, number>();
const diag2 = new Map<number, number>();
const s = new Set<number>();
for (const [i, j] of lamps) {
if (s.has(i * n + j)) {
continue;
}
s.add(i * n + j);
row.set(i, (row.get(i) || 0) + 1);
col.set(j, (col.get(j) || 0) + 1);
diag1.set(i - j, (diag1.get(i - j) || 0) + 1);
diag2.set(i + j, (diag2.get(i + j) || 0) + 1);
}
const ans: number[] = [];
for (const [i, j] of queries) {
if (
row.get(i)! > 0 ||
col.get(j)! > 0 ||
diag1.get(i - j)! > 0 ||
diag2.get(i + j)! > 0
) {
ans.push(1);
} else {
ans.push(0);
}
for (let x = i - 1; x <= i + 1; ++x) {
for (let y = j - 1; y <= j + 1; ++y) {
if (x < 0 || x >= n || y < 0 || y >= n || !s.has(x * n + y)) {
continue;
}
s.delete(x * n + y);
row.set(x, row.get(x)! - 1);
col.set(y, col.get(y)! - 1);
diag1.set(x - y, diag1.get(x - y)! - 1);
diag2.set(x + y, diag2.get(x + y)! - 1);
}
}
}
return ans;
}