bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1001. Grid Illumination

1001. Grid Illumination

Leetcode Solutions

Using Hash Maps to Track Illumination

  1. Initialize hash maps for rows, columns, positive diagonals, and negative diagonals to keep track of the number of lamps illuminating them.
  2. Initialize a set to keep track of the positions of the turned-on lamps.
  3. Iterate over the list of lamps, incrementing the corresponding counts in the hash maps and adding the lamp's position to the set.
  4. For each query, check the hash maps to determine if the cell is illuminated.
  5. If the cell is illuminated, add 1 to the result list; otherwise, add 0.
  6. After processing a query, iterate over the lamp at the query position and its 8 adjacent cells.
  7. For each lamp that is on, decrement the corresponding counts in the hash maps and remove the lamp from the set.
  8. Return the result list.
UML Thumbnail

Brute Force Approach with Grid Simulation

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...