Grid Illumination - 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-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.

Input & Output

Example 1 — Basic Illumination
$ Input: n = 5, lamps = [[0,0],[4,4]], queries = [[1,1],[1,0]]
Output: [1,0]
💡 Note: Query [1,1]: Lamp at [0,0] illuminates same main diagonal (0-0 = 1-1). Query [1,0]: Lamp at [0,0] illuminates same column 0, but after first query, nearby lamps are turned off.
Example 2 — Multiple Lamps
$ Input: n = 5, lamps = [[0,0],[4,4]], queries = [[1,1],[1,1]]
Output: [1,1]
💡 Note: First query [1,1]: Both lamps illuminate this position. Second query [1,1]: Lamps were turned off in 3x3 area, but lamp at [4,4] is still active and illuminates via diagonal.
Example 3 — No Illumination
$ Input: n = 5, lamps = [[0,0]], queries = [[4,4]]
Output: [0]
💡 Note: Query [4,4]: Lamp at [0,0] does not illuminate [4,4] (different row, column, and diagonals).

Constraints

  • 1 ≤ n ≤ 109
  • 0 ≤ lamps.length ≤ 20000
  • 0 ≤ queries.length ≤ 20000
  • lamps[i].length == 2
  • 0 ≤ rowi, coli < n

Visualization

Tap to expand
Grid Illumination Problem INPUT n = 5 Grid L L lamps = [[0,0],[4,4]] queries = [[1,1],[1,0]] Q1 Q2 Q1=[1,1] Q2=[1,0] Yellow = illuminated L = lamp position ALGORITHM STEPS 1 Initialize Hash Maps Track rows, cols, diagonals row[r], col[c], diag[r-c] anti[r+c], lamps set 2 Add Lamps to Maps For each lamp, increment count in all 4 maps Add to lamps set 3 Process Query Check if cell illuminated: row[r] OR col[c] OR diag[r-c] OR anti[r+c] > 0 4 Turn Off Adjacent Check 9 cells (3x3 area) Remove lamps found Decrement map counts Hash Maps Structure: row: {0:1, 4:1} col: {0:1, 4:1} diag: {0:2} anti: {0:1, 8:1} FINAL RESULT Query 1: [1,1] Check illumination: diag[1-1]=diag[0]=2 > 0 Cell IS illuminated! ans[0] = 1 Query 2: [1,0] After Q1: lamp[0,0] OFF row[1]=0, col[0]=0 Cell NOT illuminated ans[1] = 0 Output: [1, 0] Key Insight: Hash Map Optimization Instead of storing illumination state for each cell (O(n^2) space), use 4 hash maps to track lamp counts per row, column, main diagonal (r-c), and anti-diagonal (r+c). A cell is illuminated if ANY of its 4 corresponding map values is greater than 0. Time: O(L + Q), Space: O(L). TutorialsPoint - Grid Illumination | Hash Map Optimization Approach
Asked in
Google 15 Amazon 8 Microsoft 6
25.0K Views
Medium Frequency
~35 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen