Grid Illumination Problem
Description
LeetCode Problem 1001.
There is a 2D grid of size n x n where each cell of this grid has a lamp that is initially turned off.
You are given a 2D array of lamp positions lamps, where lamps[i] = [row_i, col_i] indicates that the lamp at grid[row_i][col_i] is turned on. Even if the same lamp is listed more than once, it is turned on.
When a lamp is turned on, it illuminates its cell and all other cells in the same row, column, or diagonal.
You are also given another 2D array queries, where queries[j] = [row_j, col_j]. For the j^th query, determine whether grid[row_j][col_j] is illuminated or not. After answering the j^th query, turn off the lamp at grid[row_j][col_j] and its 8 adjacent lamps if they exist. A lamp is adjacent if its cell shares either a side or corner with grid[row_j][col_j].
Return an array of integers ans, where ans[j] should be 1 if the cell in the j^th query was illuminated, or 0 if the lamp was not.
Example 1:

Input: n = 5, lamps = [[0,0],[4,4]], queries = [[1,1],[1,0]]
Output: [1,0]
Explanation: We have the initial grid with all lamps turned off. In the above picture we see the grid after turning on the lamp at grid[0][0] then turning on the lamp at grid[4][4].
The 0^th query asks if the lamp at grid[1][1] is illuminated or not (the blue square). It is illuminated, so set ans[0] = 1. Then, we turn off all lamps in the red square.

The 1^st query asks if the lamp at grid[1][0] is illuminated or not (the blue square). It is not illuminated, so set ans[1] = 0. Then, we turn off all lamps in the red rectangle.

Example 2:
Input: n = 5, lamps = [[0,0],[4,4]], queries = [[1,1],[1,1]]
Output: [1,1]
Example 3:
Input: n = 5, lamps = [[0,0],[0,4]], queries = [[0,4],[0,1],[1,4]]
Output: [1,1,0]
Constraints:
1 <= n <= 10^90 <= lamps.length <= 200000 <= queries.length <= 20000lamps[i].length == 20 <= row_i, col_i < nqueries[j].length == 20 <= row_j, col_j < n
Solution
Approach: Count Lamps per Line
The grid can be 109 by 109, so we cannot store it. But a cell is lit exactly when some lamp that is on shares its row, its column, its diagonal or its anti-diagonal. Each of these lines has a simple number:
- row
r, columnc; - the diagonal through
(r, c)has a constantr - c; - the anti-diagonal has a constant
r + c.
So keep four hash maps that count how many lamps are on in each row, column, diagonal and anti-diagonal, plus a set of the lamps that are on.
- For every lamp, if it is not already in the set, add it and increment its four line counts. The check matters: the same lamp can be listed several times, but it is one lamp and must be counted (and later turned off) once.
- For every query
(r, c), the cell is lit if any of the four counts for its lines is positive. - Then look at the 3 x 3 block around
(r, c). Each of those nine cells that holds a lamp is removed from the set and its four line counts are decremented.
The turn-off happens after the answer, so a query on a lamp's own cell is still lit (example 3 lights [0,4] and then switches that lamp off). The neighbor cells may fall outside the grid; looking them up in the set simply finds nothing.
Complexity: O(L + Q) expected time, where L is the number of lamps and Q the number of queries (each query does nine set lookups). O(L) extra space.
from collections import Counter
class Solution:
def gridIllumination(self, n: int, lamps: List[List[int]], queries: List[List[int]]) -> List[int]:
on = set() # lamps that are on (a lamp listed twice is still one lamp)
rows, cols, diag, anti = Counter(), Counter(), Counter(), Counter() # lamps on each line
for r, c in lamps:
if (r, c) not in on:
on.add((r, c))
rows[r] += 1
cols[c] += 1
diag[r - c] += 1
anti[r + c] += 1
ans = []
for r, c in queries:
ans.append(1 if rows[r] or cols[c] or diag[r - c] or anti[r + c] else 0)
# Turn off the lamp on this cell and its 8 neighbours.
for x in range(r - 1, r + 2):
for y in range(c - 1, c + 2):
if (x, y) in on:
on.remove((x, y))
rows[x] -= 1
cols[y] -= 1
diag[x - y] -= 1
anti[x + y] -= 1
return ans