1、题目链接
https://leetcode-cn.com/problems/prison-cells-after-n-days/
8 间牢房排成一排,每间牢房不是有人住就是空着。
每天,无论牢房是被占用或空置,都会根据以下规则进行更改:
如果一间牢房的两个相邻的房间都被占用或都是空的,那么该牢房就会被占用。
否则,它就会被空置。
(请注意,由于监狱中的牢房排成一行,所以行中的第一个和最后一个房间无法有两个相邻的房间。)
我们用以下方式描述监狱的当前状态:如果第 i 间牢房被占用,则 cell[i]==1,否则 cell[i]==0。
根据监狱的初始状态,在 N 天后返回监狱的状况(和上述 N 种变化)。
示例1:
输入:cells = [0,1,0,1,1,0,0,1], N = 7
输出:[0,0,1,1,0,0,0,0]
解释:
下表概述了监狱每天的状况:
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]
示例2:
输入:cells = [1,0,0,1,0,0,1,0], N = 1000000000
输出:[0,0,1,1,1,1,1,0]
提示:
cells.length == 8
cells[i] 的值为 0 或 1
1 <= N <= 10^9
------------------------------------------------------------------------------------------------------------------------------
2.解题思路
先寻找这个数组的循环周期,
知道循环周期之后再取余,算出来,我们的N是循环后剩余的第几次
public static int findCircle(int[] cells, int N){
//用来保存历史牢房情况
List<int[]> tempList = new ArrayList<>();
tempList.add(cells);
//每次变化后牢房的情况
int[] temp;
//记录变化的次数
int count = 0;
//是否发现重复的情况
boolean found = false;
//发现重复的时候,是第几个牢房重复的
int someIndex = 0;
while (!found){
temp = new int[cells.length];
for (int i = 1; i < cells.length-1; i++) {
if (cells[i+1]==cells[i-1]){
temp[i]=1;
}
}
cells = temp;
for (int i = 0; i < tempList.size(); i++) {
if (Arrays.equals(tempList.get(i), temp)){
someIndex = i;
System.out.println("跟第几个一样:"+someIndex);
found = true;
System.out.println(1);
break;
}
}
tempList.add(temp);
count++;
System.out.println(2);
}
System.out.println(3);
System.out.println(count-someIndex);
return count-someIndex;
}
-----------------------------------------------------------------------------------
3.代码
public class MyClass {
public static void main(String[] args) {
System.out.println(System.currentTimeMillis());
//System.out.println(new MyClass().removeKdigits( "123456",3));
int[] cells={1,1,0,0,1,0,1,1};
prisonAfterNDays(cells,100);
}
public static int findCircle(int[] cells, int N){
//用来保存历史牢房情况
List<int[]> tempList = new ArrayList<>();
tempList.add(cells);
//每次变化后牢房的情况
int[] temp;
//记录变化的次数
int count = 0;
//是否发现重复的情况
boolean found = false;
//发现重复的时候,是第几个牢房重复的
int someIndex = 0;
while (!found){
temp = new int[cells.length];
for (int i = 1; i < cells.length-1; i++) {
if (cells[i+1]==cells[i-1]){
temp[i]=1;
}
}
cells = temp;
for (int i = 0; i < tempList.size(); i++) {
if (Arrays.equals(tempList.get(i), temp)){
someIndex = i;
System.out.println("跟第几个一样:"+someIndex);
found = true;
System.out.println(1);
break;
}
}
tempList.add(temp);
count++;
System.out.println(2);
}
System.out.println(3);
System.out.println(count-someIndex);
return count-someIndex;
}
public static int[] prisonAfterNDays(int[] cells, int N) {
//用来替换第N天后的牢房情况
int[] temp;
//循环中期为14
int circle = findCircle(cells,N);
N = N %circle ;
if(N==0){
N=circle ;
}
while (N>0){
temp = new int[cells.length];
for (int i = 1; i < cells.length-1; i++) {
//如果两边的值一样,该牢房就会被占用
if (cells[i+1]==cells[i-1]){
temp[i]=1;
}
}
cells = temp;
N--;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < cells.length; i++) {
sb.append(cells[i]);
}
System.out.println("fxr"+" "+sb);
return cells;
}
}