有八个牢房。有空的,也有有犯人的。
There are 8 prison cells in a row, and each cell is either occupied or vacant.
每天,牢房要重新分配一下。规则如下
Each day, whether the cell is occupied or vacant changes according to the following rules:
如果这个牢房相邻的两个房间都是空的。或者都有人的。那么下次这个房间就要安排犯人
If a cell has two adjacent neighbors that are both occupied or both vacant, then the cell becomes occupied.
否则,下一次就要空着。
Otherwise, it becomes vacant.
(值得注意的是,牢房是排成一排的,所以第一间和最后一间并没有两个相邻的房间。)
(Note that because the prison is a row, the first and the last cells in the row can't have two adjacent neighbors.)
这个表示牢房的数组 cells, cells[i]==1 有人 cells[i]==0 没人
We describe the current state of the prison in the following way: cells[i] == 1 if the i-th cell is occupied, else cells[i] == 0.
有最初牢房的状态。求N天以后牢房是怎么安排的。
Given the initial state of the prison, return the state of the prison after N days (and N such changes described above.)
Example 1:
Input: cells = [0,1,0,1,1,0,0,1], N = 7Output: [0,0,1,1,0,0,0,0]Explanation: The following table summarizes the state of the prison on each day:Day 0: [0, 1, 0, 1, 1, 0, 0, 1]Day 1: [0, 1, 1, 0, 0, 0, 0, 0]Day 2: [0, 0, 0, 0, 1, 1, 1, 0]Day 3: [0, 1, 1, 0, 0, 1, 0, 0]Day 4: [0, 0, 0, 0, 0, 1, 0, 0]Day 5: [0, 1, 1, 1, 0, 1, 0, 0]Day 6: [0, 0, 1, 0, 1, 1, 0, 0]Day 7: [0, 0, 1, 1, 0, 0, 0, 0]
Example 2:
Input: cells = [1,0,0,1,0,0,1,0], N = 1000000000
Output: [0,0,1,1,1,1,1,0]
超时的算法~~ 算了近一分钟才跳结果。当然在leetcode那边就直接down了。
public int[] PrisonAfterNDays(int[] cells, int N) {
if(N==0) return cells;
while(N>0){
cells=PassDay(cells);
N=N-1;
}
return cells;
}
private int[] PassDay(int[] cells){
int[] res=new int[8];
for(int i=1;i<7;i++){
if(cells[i-1]==cells[i+1]){
res[i]=1;
}else{
res[i]=0;
}
}
return res;
}
如果是位运算,我就缴械投降了。。。 感觉位运算要漂移到电子电路领域。
There are 8 prison cells in a row, and each cell is either occupied or vacant.
每天,牢房要重新分配一下。规则如下
Each day, whether the cell is occupied or vacant changes according to the following rules:
如果这个牢房相邻的两个房间都是空的。或者都有人的。那么下次这个房间就要安排犯人
If a cell has two adjacent neighbors that are both occupied or both vacant, then the cell becomes occupied.
否则,下一次就要空着。
Otherwise, it becomes vacant.
(值得注意的是,牢房是排成一排的,所以第一间和最后一间并没有两个相邻的房间。)
(Note that because the prison is a row, the first and the last cells in the row can't have two adjacent neighbors.)
这个表示牢房的数组 cells, cells[i]==1 有人 cells[i]==0 没人
We describe the current state of the prison in the following way: cells[i] == 1 if the i-th cell is occupied, else cells[i] == 0.
有最初牢房的状态。求N天以后牢房是怎么安排的。
Given the initial state of the prison, return the state of the prison after N days (and N such changes described above.)
Example 1:
Input: cells = [0,1,0,1,1,0,0,1], N = 7Output: [0,0,1,1,0,0,0,0]Explanation: The following table summarizes the state of the prison on each day:Day 0: [0, 1, 0, 1, 1, 0, 0, 1]Day 1: [0, 1, 1, 0, 0, 0, 0, 0]Day 2: [0, 0, 0, 0, 1, 1, 1, 0]Day 3: [0, 1, 1, 0, 0, 1, 0, 0]Day 4: [0, 0, 0, 0, 0, 1, 0, 0]Day 5: [0, 1, 1, 1, 0, 1, 0, 0]Day 6: [0, 0, 1, 0, 1, 1, 0, 0]Day 7: [0, 0, 1, 1, 0, 0, 0, 0]
Example 2:
Input: cells = [1,0,0,1,0,0,1,0], N = 1000000000
Output: [0,0,1,1,1,1,1,0]
超时的算法~~ 算了近一分钟才跳结果。当然在leetcode那边就直接down了。
public int[] PrisonAfterNDays(int[] cells, int N) {
if(N==0) return cells;
while(N>0){
cells=PassDay(cells);
N=N-1;
}
return cells;
}
private int[] PassDay(int[] cells){
int[] res=new int[8];
for(int i=1;i<7;i++){
if(cells[i-1]==cells[i+1]){
res[i]=1;
}else{
res[i]=0;
}
}
return res;
}
如果是位运算,我就缴械投降了。。。 感觉位运算要漂移到电子电路领域。