1046. Light Panel Presses
A control room has a rectangular wall of lamps, where grid[r][c] is true when the lamp in row r and column c is on. Every entry of presses is a pair [r, c], and pressing that button flips the lamp at (r, c) together with its neighbours directly above, below, left and right, wherever those exist on the wall.
Apply the presses in the order given and return the final state of the wall. The input grid should be treated as the starting state. Each press is independent, so pressing the same cell twice restores everything it touched. Simulate in O(presses) time on top of copying the grid.
Example 1
- Input:
- grid = [[false,false,false],[false,false,false],[false,false,false]]presses = [[1,1]]
- Output:
- [[false,true,false],[true,true,true],[false,true,false]]
- Explanation:
The single press flips the centre lamp and its four neighbours, so a plus shape is lit.
Example 2
- Input:
- grid = [[true,true],[true,true]]presses = [[0,0],[1,1]]
- Output:
- [[false,true],[true,false]]
- Explanation:
The first press clears the top-left lamp and its two neighbours, and the second press flips the bottom-right lamp and its two neighbours, leaving the diagonal off and the other two lit.
Constraints
1 ≤ rows, cols ≤ 30
0 ≤ presses.length ≤ 1000
0 ≤ r < rows, 0 ≤ c < cols for every press (0-indexed)
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(rows*cols + presses)
- Space
- O(rows*cols)