↓Skip to main content
  1. LeetCode/

LeetCode 1001: Grid Illumination

·3 mins· ·
LeetCode Hard Hash-Table Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: hard First Attempt: 2026-10-03

  • Submissions: Runtime Error → Wrong Answer → Accepted → Wrong Answer ×2 → Accepted

Approach
#

n can reach 10^9, so no grid. A lamp lights its row, column and both diagonals, so count “how many lit lamps” along each of the four directions (Hash Table Template 3):

  • Row r, column c, diagonal r - c, anti-diagonal r + c.
  • A cell is lit when any of the four counts is > 0.
  • A set records which lamps are on: a lamp can be listed twice but counts once, and only lamps that are on can be turned off.

After each query, check the cell and its 8 neighbors; turn off any that are on, decrementing all four counters.

My Attempts
#

VersionProblem
REif row, col not in active_lamps was missing parentheses, a syntax error
WAThe query used row_count[col] instead of col_count[col], and turning off decremented row_count[nc] too. For n = 3, a lamp at (0, 2) and query (1, 2): same column, answer 1, it returned 0
ACRewrote the whole thing with all four counters right
Two WAsWent back to the original code and fixed the query first, but turning off still decremented row_count[nc], so column counts never dropped. For n = 4, a lamp at (1, 3), queries (0, 3) then (2, 3): the first query turns the lamp off, so the second answer is 0; it returned 1
ACChanged to col_count[nc] -= 1

Solution
#

class Solution:
    def gridIllumination(self, n: int, lamps: List[List[int]], queries: List[List[int]]) -> List[int]:
        row, col, diag, anti = Counter(), Counter(), Counter(), Counter()
        on = set()
        for r, c in lamps:
            if (r, c) not in on:
                on.add((r, c))
                row[r] += 1; col[c] += 1; diag[r - c] += 1; anti[r + c] += 1
        res = []
        for r, c in queries:
            res.append(1 if row[r] or col[c] or diag[r - c] or anti[r + c] else 0)
            for nr in (r - 1, r, r + 1):
                for nc in (c - 1, c, c + 1):
                    if (nr, nc) in on:
                        on.remove((nr, nc))
                        row[nr] -= 1; col[nc] -= 1; diag[nr - nc] -= 1; anti[nr + nc] -= 1
        return res

Complexity
#

Time O(L + Q) for L lamps and Q queries (9 cells per query), space O(L).

Mistakes to Avoid
#

  • The four counter names look alike; after copy-paste, check each one in both the query and the turn-off.
  • A repeated lamp counts once.

Interview Explanation
#

The grid is too large to simulate, so I count lamps per row, column, r - c diagonal and r + c anti-diagonal, with a set of lamps that are on. A query is lit if any of the four counts is positive; afterward I check the 9 surrounding cells and turn off any lit lamp, updating all four counters. Time O(L + Q).

Related

LeetCode 3873: Maximum Points Activated with One Addition
·2 mins
LeetCode Hard Union-Find Hash-Table Array
LeetCode 3873 note: merge points sharing an x or y into components; a new point joins at most two of them, so the answer is the two largest plus 1
LeetCode 41: First Missing Positive
·3 mins
LeetCode Hard Hash-Table Array
LeetCode 41 note: the answer is in 1..n+1; replace impossible values with n+1 (not 0) and mark v as seen with the sign of nums[v-1]
LeetCode 2122: Recover the Original Array
·3 mins
LeetCode Hard Two-Pointers Sorting Hash-Table Enumeration Array
LeetCode 2122 note: the smallest number is always in lower, so enumerating its partner fixes k; after sorting, two pointers pair each smallest unmatched number with the number 2k above it
LeetCode 2009: Minimum Number of Operations to Make Array Continuous
·2 mins
LeetCode Hard Sliding-Window Hash-Table Binary-Search Array
LeetCode 2009 note: the result is n consecutive integers; after deduplicating and sorting, a sliding window finds the most distinct values within a span of n - 1, and the rest must change
LeetCode 952: Largest Component Size by Common Factor
·2 mins
LeetCode Hard Math Number-Theory Union-Find Hash-Table Array
LeetCode 952 note: sharing a factor ⇔ sharing a prime factor; factor each number and union indices with the same prime factor using Union-Find, with no pairwise GCDs
LeetCode 2963: Count the Number of Good Partitions
·2 mins
LeetCode Hard Math Combinatorics Hash-Table Array
LeetCode 2963 note: all occurrences of a value must share a part, so last occurrences cut the array into b indivisible blocks; each boundary may be cut or not, giving 2^(b-1)