-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
44 lines (32 loc) · 1.1 KB
/
Solution.java
File metadata and controls
44 lines (32 loc) · 1.1 KB
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
/**
@lc id : 957 Prison Cells After N Days
@author : rohit
@date : 03/07/2020
@url : https://leetcode.com/problems/prison-cells-after-n-days/
*/
class Solution {
public int[] prisonAfterNDays(int[] cells, int n) {
HashMap<String, Integer> map = new HashMap<>();
for(int i = 0; i < n; i++){
String state = Arrays.toString(cells);
if(map.containsKey(state)){
int loopLength = i - map.get(state);
int remainingDays = (n - i) % loopLength;
return prisonAfterNDays(cells, remainingDays);
}
else{
map.put(state, i);
int prev = cells[0];
for(int j = 1; j < 7; j++){
int next = cells[j+1];
int curr = cells[j];
cells[j] = prev == next ? 1 : 0;
prev = curr;
}
}
cells[0] = 0;
cells[7] = 0;
}
return cells;
}
}