> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Grid Illumination Python Solution with Tests

> Tested Python solution for LeetCode 1001 with 22 pytest cases. Generate a practice environment with lcpy.

LeetCode 1001, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table). [View on LeetCode](https://leetcode.com/problems/grid-illumination/description/).

Generate this problem as a practice environment: tested reference solution, 22 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1001   # by problem number
lcpy gen -s grid_illumination   # by problem name
```

## Problem

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<sup>th</sup>` query, determine whether `grid[row_j][col_j]` is illuminated or not. After answering the `j<sup>th</sup>` 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<sup>th</sup>` query was illuminated, or `0` if the lamp was not.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2020/08/19/illu_1.jpg)

```
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 0th 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.
```

```
Input: n = 5, lamps = [[0,0],[4,4]], queries = [[1,1],[1,1]]
Output: [1,1]
```

```
Input: n = 5, lamps = [[0,0],[0,4]], queries = [[0,4],[0,1],[1,4]]
Output: [1,1,0]
```

### Constraints

* 1 \<= n \<= 10^9
* 0 \<= lamps.length \<= 20000
* 0 \<= queries.length \<= 20000
* lamps\[i].length == 2
* 0 \<= row\_i, col\_i \< n
* queries\[j].length == 2
* 0 \<= row\_j, col\_j \< n

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/grid_illumination/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/grid_illumination/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(L + Q) where L = len(lamps), Q = len(queries) (9 turns per query)
    # Space: O(L)
    def grid_illumination(
        self, n: int, lamps: list[list[int]], queries: list[list[int]]
    ) -> list[int]:
        lit: set[tuple[int, int]] = set()
        for r, c in lamps:
            lit.add((r, c))
        rows: dict[int, int] = {}
        cols: dict[int, int] = {}
        diag: dict[int, int] = {}
        anti: dict[int, int] = {}
        for r, c in lit:
            rows[r] = rows.get(r, 0) + 1
            cols[c] = cols.get(c, 0) + 1
            diag[r - c] = diag.get(r - c, 0) + 1
            anti[r + c] = anti.get(r + c, 0) + 1

        result: list[int] = []
        for r, c in queries:
            is_lit = (
                rows.get(r, 0) > 0
                or cols.get(c, 0) > 0
                or diag.get(r - c, 0) > 0
                or anti.get(r + c, 0) > 0
            )
            result.append(1 if is_lit else 0)
            for dr in (-1, 0, 1):
                for dc in (-1, 0, 1):
                    lamp = (r + dr, c + dc)
                    if lamp in lit:
                        lit.remove(lamp)
                        rows[lamp[0]] -= 1
                        cols[lamp[1]] -= 1
                        diag[lamp[0] - lamp[1]] -= 1
                        anti[lamp[0] + lamp[1]] -= 1
        return result
```

## Complexity

| Time | Space |
| - | - |
| O(L + Q) where L = len(lamps), Q = len(queries) (9 turns per query) | O(L) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.