forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrid Illumination.py
36 lines (31 loc) · 1.18 KB
/
Grid Illumination.py
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
class Solution:
def gridIllumination(self, n: int, lamps: List[List[int]], queries: List[List[int]]) -> List[int]:
rows = collections.Counter()
cols = collections.Counter()
diags1 = collections.Counter()
diags2 = collections.Counter()
lamps = {tuple(lamp) for lamp in lamps}
for i, j in lamps:
rows[i] += 1
cols[j] += 1
diags1[i + j] += 1
diags2[i - j] += 1
ans = []
directions = ((-1, -1), (-1, 0), (-1, 1),
(0, -1), (0, 0), (0, 1),
(1, -1), (1, 0), (1, 1))
for i, j in queries:
if rows[i] or cols[j] or diags1[i + j] or diags2[i - j]:
ans.append(1)
else:
ans.append(0)
for di, dj in directions:
newI, newJ = i + di, j + dj
if (newI, newJ) not in lamps:
continue
lamps.remove((newI, newJ))
rows[newI] -= 1
cols[newJ] -= 1
diags1[newI + newJ] -= 1
diags2[newI - newJ] -= 1
return ans