1. 程式人生 > >[Swift Weekly Contest 115]LeetCode957. N 天后的牢房 | Prison Cells After N Days

[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 }