You are given an m x n integer matrix neurons.
-
A cell is a
firing
neuron if its value is
0
.
-
A cell is a
non-firing
neuron if its value is greater than
0
.
For each cell, count the number of firing neighbors among its 8 surrounding cells (horizontal, vertical, and diagonal). All updates must be applied simultaneously based on the original state of the matrix.
Update the matrix using these rules:
-
If a cell is firing (
0
) and
exactly 3
of its neighbors are firing, its new value becomes
6
.
-
If a cell is non-firing and it has
0 or 1
firing neighbors, decrease its value by
2
.
-
If a cell is non-firing and it has
more than 3
firing neighbors, decrease its value by
1
.
-
A cell's value can never go below
0
.
-
In all other cases, the cell keeps its current value.
Implement a function to update the matrix state.
Follow-up:
-
First solve it using a copied matrix.
-
Then optimize the solution to use
O(1)
auxiliary space.