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 onqueries: Array of room positions to check for illumination
For each query, you need to:
- Check if the room is illuminated (by any lamp's row/column/diagonal coverage)
- 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
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!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code