Advertisement

Grid Illumination - LeetCode 1001 Solution

Grid Illumination - Complete Solution Guide

Grid Illumination is LeetCode problem 1001, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 ,

Detailed Explanation

The problem asks you to simulate a grid of lamps that can illuminate rows, columns, and diagonals. You're given an initial set of lit lamps and a series of queries. Each query asks if a particular cell is illuminated. If it is, you record '1', otherwise '0'. After answering each query, you turn off the lamp at the queried cell and its 8 adjacent neighbors (if they exist). The goal is to return an array representing the answers to each query.

Solution Approach

The solution uses a combination of hash tables (or counters) and a set to efficiently track the state of illuminated rows, columns, diagonals, and the lit lamps themselves. The algorithm iterates through the queries. For each query, it checks if the cell is illuminated based on the counts in the hash tables. Then, it turns off the lamp at the query cell and its 8 neighbors by decrementing the counts in the hash tables and removing the corresponding lamp positions from the set.

Step-by-Step Algorithm

  1. Step 1: Initialize a set `lamp_positions` to store the coordinates of all lit lamps, to avoid duplicates, and four hash tables (`row_counts`, `col_counts`, `diag1_counts`, `diag2_counts`) to store the counts of illuminated rows, columns, and diagonals, respectively.
  2. Step 2: Iterate through the `lamps` array. For each lamp, if its position isn't already in `lamp_positions`, add it to the set and increment the corresponding counts in `row_counts`, `col_counts`, `diag1_counts`, and `diag2_counts`. `diag1_counts` tracks r - c, representing one diagonal family, while `diag2_counts` tracks r + c, representing the other diagonal family.
  3. Step 3: Initialize an empty list `ans` to store the results of each query.
  4. Step 4: Iterate through the `queries` array. For each query, check if the cell `(qr, qc)` is illuminated by checking if `row_counts[qr] > 0`, `col_counts[qc] > 0`, `diag1_counts[qr - qc] > 0`, or `diag2_counts[qr + qc] > 0`. Append 1 to `ans` if illuminated, and 0 otherwise.
  5. Step 5: Turn off the lamp at `(qr, qc)` and its 8 adjacent neighbors. Iterate through a 3x3 grid centered at `(qr, qc)`. For each neighbor `(nr, nc)`, check if the lamp is lit (exists in `lamp_positions`). If it is, remove it from `lamp_positions` and decrement the corresponding counts in `row_counts`, `col_counts`, `diag1_counts`, and `diag2_counts`.
  6. Step 6: After processing all queries, return the `ans` list.

Key Insights

  • Insight 1: Using direct grid representation is not feasible due to potentially huge grid size (up to 10^9). Therefore, storing the state of each cell individually is space inefficient. Hash tables are suitable for storing the states of lit lamps, rows, columns, and diagonals.
  • Insight 2: Instead of representing the entire grid, we only need to keep track of the lit lamps and the counts of illuminated rows, columns, and diagonals using hash tables (or counters).
  • Insight 3: Efficiently managing the lamps to be turned off after each query is crucial. A set can be used to quickly check and remove lamps that need to be turned off during the query process.

Complexity Analysis

Time Complexity: O(L + Q)

Space Complexity: O(L)

Topics

This problem involves: Array, Hash Table.

Companies

Asked at: Dropbox.