-
Notifications
You must be signed in to change notification settings - Fork 41
/
solution.js
34 lines (32 loc) · 941 Bytes
/
solution.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* @param {number[]} cells
* @param {number} N
* @return {number[]}
*/
var prisonAfterNDays = function (cells, N) {
const map = {};
const arr = [];
const key = JSON.stringify(cells);
map[key] = 0;
arr[0] = key;
for (let i = 0; i < N; i++) {
const nextCells = new Array(cells.length);
nextCells[0] = 0;
nextCells[nextCells.length - 1] = 0;
for (let j = 1; j < nextCells.length - 1; j++) {
nextCells[j] = cells[j - 1] === cells[j + 1] ? 1 : 0;
}
const key = JSON.stringify(nextCells);
if (map[key] === undefined) {
map[key] = i + 1;
arr[i + 1] = key;
} else {
// lastIndex = map[key];
const countLoop = i - map[key] + 1;
const mod = (N - i - 1) % countLoop;
return JSON.parse(arr[map[key] + mod]);
}
cells = nextCells;
}
return cells;
};