[LeetCode] 957. Prison Cells After N Days

题:https://leetcode.com/problems/prison-cells-after-n-days/

题目

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.)

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.

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 = 7
Output: [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]

*Note:

  1. cells.length == 8
  2. cells[i] is in {0, 1}
  3. 1 <= N <= 10^9

题目大意

给定一个迭代规则,求 N次迭代后的结果。
1.next[i] = 1 if (cur[i-1] == 0 && cur[i+1] == 0 ) || (cur[i-1] == 1 && cur[i+1] == 1 )
2.next[0] = 0 , next[7] = 0 ;

思路

N = 1000000000

就算 是 T(N) 也会超时。
所以 方法是 减少 相应的 计算 总量,即 N 转化为 n。
考虑到 是一种规则变化,所以可以试试 看是否 是循环变化 ,即 存在 相应的 最小周期。

由于 起始数组 ,nums[0] 和nums[7] 可能不为零,必不会成为 循环的一环,所以从 nums1(nums迭代一次)开始迭代,寻找相应的最小周期,然后 将N 简化为 n。

class Solution {
    public int[] prisonAfterNDays(int[] cells, int N) {
        int[] ocells = new int[cells.length];
        int[] fcells = new int[cells.length];
        for(int i = 1;i<=6;i++){
            ocells[i] = 1- cells[i-1]^cells[i+1];
            fcells[i] =  ocells[i];
        }
        int[] ccells = new int[cells.length];
       int cnt = 1 ;
       while(true) {
           for (int i = 1; i <= 6; i++)
               ccells[i] = 1 - fcells[i - 1] ^ fcells[i + 1];
           for (int i = 1; i <= 6; i++)
               fcells[i] = ccells[i];
           boolean isSame = true;
           for (int i = 1; i <= 6; i++) {
               if (ccells[i] != ocells[i]) {
                   isSame = false;
                   break;
               }
           }
           if (isSame)
               break;
           cnt++;
       }
        int n = (N-1)%cnt;
        while(n-->0){
            for (int i = 1; i <= 6; i++)
                ccells[i] = 1 - fcells[i - 1] ^ fcells[i + 1];
            for (int i = 1; i <= 6; i++)
                fcells[i] = ccells[i];
        }
        return fcells;


    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值