This problem looks busy at first, but the accepted solution is built around one steady invariant. For 957. Prison Cells After N Days, the solution in this repository is mainly a dynamic programming solution.
Guide
What?
Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: dynamic programming, bit manipulation.
The notes already sitting in the source point us in the right direction:
- iteration + memorization
- TLE
- 1 / 258 test cases passed.
Guide
When?
Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are prisonAfterNDays, memo, getNext, next, toNum.
Guide
Why?
The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.
- A map keeps the lookup side cheap; the code pays a little memory to avoid repeated searching.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(1), space: O(1)
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//iteration + memorization
02//TLE
03//1 / 258 test cases passed.
04class Solution {
05public:
06 vector<int> prisonAfterNDays(vector<int>& cells, int N) {
07 int n = cells.size(), state = 0;
08 vector<int> memo(1<<n);
09
10 for(int i = 0; i < n; i++){
11 state += (cells[i] << i);
12 }
13
14 while(N-- > 0){
15 if(memo[state] != 0){
16 state = memo[state];
17 continue;
18 }
19
20 int tmp = 0;
21
22 for(int i = 1; i < n-1; ++i){
23 // cout << i << ": " << (state&(1<<(i-1))) << ", " << (state&(1<<(i+1))) << endl;
24 if((bool)(state&(1<<(i-1))) == (bool)(state&(1<<(i+1)))){
25 tmp |= (1 << i);
26 // cout << i << " ";
27 }
28 }
29 // cout << endl;
30
31 state = memo[state] = tmp;
32 }
33
34 for(int i = 0; i < n; i++){
35 cells[i] = ((state & (1 << i)) > 0);
36 }
37
38 return cells;
39 }
40};
41
42//https://leetcode.com/problems/prison-cells-after-n-days/discuss/266854/Java%3A-easy-to-understand
43//time: O(1), space: O(1)
44//Runtime: 20 ms, faster than 10.15% of C++ online submissions for Prison Cells After N Days.
45//Memory Usage: 12.6 MB, less than 23.83% of C++ online submissions for Prison Cells After N Days.
46class Solution {
47public:
48 vector<int> getNext(vector<int>& cells){
49 int n = cells.size();
50 vector<int> next(n, 0);
51
52 for(int i = 1; i < n-1; ++i){
53 next[i] = (cells[i-1]==cells[i+1]);
54 }
55
56 return next;
57 };
58
59 int toNum(vector<int>& cells){
60 int n = cells.size();
61 int num = 0;
62
63 for(int i = 0; i < n; ++i){
64 num += (1 << i) * cells[i];
65 }
66
67 return num;
68 }
69
70 vector<int> prisonAfterNDays(vector<int>& cells, int N) {
71 int key;
72 unordered_map<int, int> state2index;
73 int cycle, remaining;
74
75 for(int i = 0; i < N; ++i){
76 key = toNum(cells);
77 if(state2index.find(key) != state2index.end()){
78 //cycle's size: current index - index of previous same state
79 cycle = i - state2index[key];
80 //have passed days: [0,i-1], remaining days: [i,N-1]
81 remaining = (N-i)%cycle;
82 break;
83 }
84
85 state2index[key] = i;
86 cells = getNext(cells);
87 // for(int cell : cells){
88 // cout << cell << " ";
89 // }
90 // cout << endl;
91 }
92
93 while(remaining-- > 0){
94 cells = getNext(cells);
95 // for(int cell : cells){
96 // cout << cell << " ";
97 // }
98 // cout << endl;
99 }
100
101 return cells;
102 }
103};
Cost