-
Notifications
You must be signed in to change notification settings - Fork 0
/
957_Prison-Cells-After-N-Days.cpp
46 lines (40 loc) · 1.13 KB
/
957_Prison-Cells-After-N-Days.cpp
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
35
36
37
38
39
40
41
42
43
44
45
46
class Solution {
public:
vector<int> prisonAfterNDays(vector<int>& cells, int n) {
int t = 0;
bitset<8> prison;
for(int i = 0; i < 8; ++i){
if(cells[i] == 1) prison.set(i);
if(cells[i] == 0) prison.reset(i);
}
unordered_map<bitset<8>,int> pattern;
vector<bitset<8>> hh;
while(!pattern.count(prison)){
hh.push_back(prison);
bitset<8> org = prison;
pattern[prison] = t++;
prison.reset(0);
prison.reset(7);
for(int i = 1; i < 7; ++i){
if(org[i+1] == org[i-1]) prison.set(i);
else prison.reset(i);
}
}
int end_loop = t - 1;
int start_loop = pattern[prison];
int loop_size = end_loop - start_loop + 1;
vector<int> output(8);
bitset<8> temp;
if(n < t){
temp = hh[n];
}else{
int a = n - start_loop;
int b = (a % loop_size);
temp = hh[b + start_loop];
}
for(int i = 0; i < 8; ++i){
output[i] = temp[i];
}
return output;
}
};