[Swift Weekly Contest 115]LeetCode957. N 天后的牢房 | 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

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]

提示:

  1. cells.length == 8
  2. cells[i] 的值为 0 或 1 
  3. 1 <= N <= 10^9

16ms
 1 class Solution {
 2     func prisonAfterNDays(_ cells: [Int], _ N: Int) -> [Int] {
 3         var cells = cells
 4         var N:Int = N % 14
 5         if N == 0
 6         { 
 7             N = 14
 8         }
 9         for i in 0..<N
10         {
11             var temp:[Int] = [Int](repeating:0,count:cells.count)
12             temp[0] = 0
13             temp[7] = 0
14             for j in 1..<7
15             {
16                  temp[j] = 1 - cells[j - 1]^cells[j + 1]
17             }
18             cells = temp
19         }
20         return cells
21     }
22 }

20ms

 1 class Solution {
 2     func prisonAfterNDays(_ cells: [Int], _ N: Int) -> [Int] {
 3         var arr:[Int] = [Int](repeating:0,count:512)
 4         var pre:[Int] = cells
 5         var cur:[Int] = [Int]()
 6         arr[getSum(pre)] = 0
 7         for i in 1...N
 8         {
 9             cur = [Int](repeating:0,count:8)
10             for j in 1..<7
11             { 
12                 if pre[j - 1] == pre[j + 1]
13                 {
14                     cur[j] = 1
15                 }
16             }
17             if arr[getSum(cur)] == 0
18             {
19                 arr[getSum(cur)] = i
20             }
21             else
22             {
23                 var p:Int = arr[getSum(cur)]
24                 var d:Int = (N - i) % (i - p)
25                 for k in 0..<512
26                 {
27                     if arr[k] == p + d
28                     {
29                         return getArr(k)
30                     }
31                 }
32             }
33             pre = cur
34         }
35         return pre
36     }
37     
38     func getArr(_ n:Int) -> [Int]
39     {
40         var n = n
41         var result:[Int] = [Int](repeating:0,count:8)
42         for i in (0...7).reversed()
43         {
44             let num:Int = Int(pow(2, Double(i)))
45             if n >= num
46             {
47                 result[i] = 1
48                 n -= num
49             }
50         }
51         return result
52     }
53     
54     func getSum(_ arr:[Int])->Int
55     {
56         var result:Int = 0
57         for  i in 0...7
58         {
59             if arr[i] == 1
60             {
61                 result += Int(pow(2, Double(i)))
62             }
63         }
64         return result
65     }
66 }

猜你喜欢

转载自www.cnblogs.com/strengthen/p/10126178.html
今日推荐