Tap here to switch tabs
Problem
Submissions

Problem: Grid Illumination

med
30 min
Try to solve the Grid Illumination problem.

Statement

Given an integer nn, consider an n×nn \times n grid of light bulbs that are initially all turned off.

You are given a 22D array of lamp positions lamps, where lamps[i] = [row_i, col_i] indicates that there is an active lamp at cell [row_i, col_i]. Each active lamp illuminates every cell in its row, its column, and both of its diagonals.

You are also given a 22D array of query coordinates queries, where queries[j] = [row_j, col_j]. For the jjth query:

  1. Check whether cell [row_j, col_j] is currently illuminated. If it is illuminated, the answer is 11; otherwise, the answer is 00.

  2. Turn off any lamp situated at [row_j, col_j] or in any of its 88 neighboring cells (horizontal, vertical, and diagonal neighbors sharing a common vertex or edge with [row_j, col_j]).

Return an array of integers representing the answers to all queries in order.

Constraints:

  • 1≤n≤1091 \le n \le 10^9

  • 0≤0 \le lamps.length ≤20000\le 20000

  • lamps[i].length =2= 2

  • 0≤rowi,coli<n0 \leq \text{row}_i, \text{col}_i < n

  • 0≤0 \le queries.length ≤20000\le 20000

  • queries[j].length =2= 2

  • 0≤rowj,colj<n0 \leq \text{row}_j, \text{col}_j < n

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Grid Illumination

med
30 min
Try to solve the Grid Illumination problem.

Statement

Given an integer nn, consider an n×nn \times n grid of light bulbs that are initially all turned off.

You are given a 22D array of lamp positions lamps, where lamps[i] = [row_i, col_i] indicates that there is an active lamp at cell [row_i, col_i]. Each active lamp illuminates every cell in its row, its column, and both of its diagonals.

You are also given a 22D array of query coordinates queries, where queries[j] = [row_j, col_j]. For the jjth query:

  1. Check whether cell [row_j, col_j] is currently illuminated. If it is illuminated, the answer is 11; otherwise, the answer is 00.

  2. Turn off any lamp situated at [row_j, col_j] or in any of its 88 neighboring cells (horizontal, vertical, and diagonal neighbors sharing a common vertex or edge with [row_j, col_j]).

Return an array of integers representing the answers to all queries in order.

Constraints:

  • 1≤n≤1091 \le n \le 10^9

  • 0≤0 \le lamps.length ≤20000\le 20000

  • lamps[i].length =2= 2

  • 0≤rowi,coli<n0 \leq \text{row}_i, \text{col}_i < n

  • 0≤0 \le queries.length ≤20000\le 20000

  • queries[j].length =2= 2

  • 0≤rowj,colj<n0 \leq \text{row}_j, \text{col}_j < n