Grid Illumination - Problem

Imagine you're managing a smart lighting system in a massive n ร— n grid building where each room has a special lamp. Initially, all lamps are turned off, but when activated, these aren't ordinary lamps - they're super-powered!

Each lamp illuminates not just its own room, but entire rows, columns, and both diagonals that pass through it. Think of it like placing a queen on a chessboard - it controls everything in its line of sight!

You're given:

  • lamps: Array of lamp positions that are initially turned on
  • queries: Array of room positions to check for illumination

For each query, you need to:

  1. Check if the room is illuminated (by any lamp's row/column/diagonal coverage)
  2. Turn off the queried lamp (if it exists) plus all 8 adjacent lamps

Goal: Return an array where 1 means the room was illuminated, 0 means it wasn't.

Input & Output

example_1.py โ€” Basic illumination
$ Input: n = 5, lamps = [[0,0],[4,4]], queries = [[1,1],[1,0]]
โ€บ Output: [1,1]
๐Ÿ’ก Note: Query (1,1): Lamp at (0,0) illuminates main diagonal including (1,1). Query (1,0): Lamp at (0,0) illuminates column 0 including (1,0). After first query, lamp at (0,0) is turned off, but second query still illuminated by remaining coverage.
example_2.py โ€” Multiple coverage
$ Input: n = 5, lamps = [[0,0],[4,4]], queries = [[1,1],[1,1]]
โ€บ Output: [1,0]
๐Ÿ’ก Note: First query (1,1): Both lamps contribute to illumination via diagonals. After query, lamp at (1,1) and neighbors are turned off (including (0,0)). Second query (1,1): Only lamp at (4,4) remains, which doesn't illuminate (1,1).
example_3.py โ€” Edge case: no illumination
$ Input: n = 5, lamps = [[0,0]], queries = [[4,4]]
โ€บ Output: [0]
๐Ÿ’ก Note: Query (4,4): Lamp at (0,0) illuminates row 0, column 0, and main diagonal (r-c=0). Position (4,4) has r-c=0 so it's on the main diagonal - this should return [1]. The lamp is then turned off along with its neighbors.

Constraints

  • 1 โ‰ค n โ‰ค 109
  • 0 โ‰ค lamps.length โ‰ค 20000
  • 0 โ‰ค queries.length โ‰ค 20000
  • lamps[i].length == 2
  • queries[j].length == 2
  • 0 โ‰ค rowi, coli, rowj, colj < n

Visualization

Tap to expand
QControl System StatusRow 0: โ–ˆโ–ˆโ–ˆโ–ˆ [1 lamp active]Col 0: โ–ˆโ–ˆโ–ˆโ–ˆ [1 lamp active]Diagonal 0: โ–ˆโ–ˆโ–ˆโ–ˆ [1 lamp]Query Row 2: โ–ˆโ–ˆโ–ˆโ–ˆ [0 active]โšก O(1) Status Check CompleteSmart Grid IlluminationCounter-based tracking enables instant O(1) queries
Understanding the Visualization
1
Install Smart Tracking
Instead of monitoring each lamp individually, install sensors that count total illumination in each row, column, and diagonal pathway
2
Instant Status Check
When security queries a location, instantly check if any pathway sensor shows illumination > 0
3
Efficient Maintenance
When turning off lamps, simply decrement the pathway counters rather than scanning all lamps
Key Takeaway
๐ŸŽฏ Key Insight: By tracking illumination counts per row/column/diagonal instead of individual lamps, we transform an O(L) problem into O(1) queries!
Asked in
Google 45 Amazon 38 Meta 25 Microsoft 22
28.4K Views
Medium-High Frequency
~25 min Avg. Time
892 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