Skip to content

Commit bdf12f8

Browse files
author
iamminji
committed
[20200704] Solve july challenge questions
1 parent f9eadcf commit bdf12f8

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
## 문제
2+
There are 8 prison cells in a row, and each cell is either occupied or vacant.
3+
4+
Each day, whether the cell is occupied or vacant changes according to the following rules:
5+
6+
If a cell has two adjacent neighbors that are both occupied or both vacant, then the cell becomes occupied.
7+
Otherwise, it becomes vacant.
8+
(Note that because the prison is a row, the first and the last cells in the row can't have two adjacent neighbors.)
9+
10+
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.
11+
12+
Given the initial state of the prison, return the state of the prison after N days (and N such changes described above.)
13+
14+
15+
```
16+
Example 1:
17+
18+
Input: cells = [0,1,0,1,1,0,0,1], N = 7
19+
Output: [0,0,1,1,0,0,0,0]
20+
Explanation:
21+
The following table summarizes the state of the prison on each day:
22+
Day 0: [0, 1, 0, 1, 1, 0, 0, 1]
23+
Day 1: [0, 1, 1, 0, 0, 0, 0, 0]
24+
Day 2: [0, 0, 0, 0, 1, 1, 1, 0]
25+
Day 3: [0, 1, 1, 0, 0, 1, 0, 0]
26+
Day 4: [0, 0, 0, 0, 0, 1, 0, 0]
27+
Day 5: [0, 1, 1, 1, 0, 1, 0, 0]
28+
Day 6: [0, 0, 1, 0, 1, 1, 0, 0]
29+
Day 7: [0, 0, 1, 1, 0, 0, 0, 0]
30+
31+
Example 2:
32+
33+
Input: cells = [1,0,0,1,0,0,1,0], N = 1000000000
34+
Output: [0,0,1,1,1,1,1,0]
35+
```
36+
37+
Note:
38+
- cells.length == 8
39+
- cells[i] is in {0, 1}
40+
- 1 <= N <= 10^9
41+
42+
### 솔루션
43+
양 쪽 값이 다르면 0, 같으면 1이라는 말을 문제에서 길게 설명하고 있다.
44+
우선 맨 끝 값들 cells[0], cells[7] 은 참고할 값이 한 개 밖에 없으므로 무조건 0이 들어간다.
45+
46+
그리고 나머지 cell 들은 좌우 값을 비교해서 넣어주면 되는데, 그냥 계산하면 Time Limit Exceeded 이 뜬다. (cell 비교가 6번 밖에 일어나지 않지만, 주어진 N 최대 값이 10^9 이다.)
47+
48+
N 값이 크기 때문에 규칙을 찾아야 하는 문제였다. 좌/우 비교 시 값이 변경 되기 때문에, 어느 순간 같은 패턴만 등장할 것이라고 추측 했다.
49+
그래서 100개 씩 돌려보니 14번마다 같은 값이 나오더라. (공식은 모르겠음...)
50+
51+
문제에서 Day 라고 주어진 부분을 키로 쓰고, 값을 cell 을 넣었다. 그 후 N 으로 들어오는 값을 14로 나누어 (모듈러 연산) 리턴했더니 풀렸다(????)
52+
53+
> IDE 로 했으니 찾았지, 손으로 했으면.. 패턴을 찾을 수 있었을까..?
54+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from typing import List
2+
from copy import copy
3+
from collections import defaultdict
4+
5+
6+
class Solution:
7+
def prisonAfterNDays(self, cells: List[int], N: int) -> List[int]:
8+
# cells size is fixed
9+
10+
result = defaultdict(list)
11+
12+
for i in range(15):
13+
result[i] = copy(cells)
14+
org = copy(cells)
15+
for j in range(1, 7):
16+
if org[j - 1] != org[j + 1]:
17+
cells[j] = 0
18+
else:
19+
cells[j] = 1
20+
21+
cells[0] = 0
22+
cells[7] = 0
23+
24+
return result[N % 14]
25+
26+
27+
if __name__ == '__main__':
28+
sol = Solution()
29+
30+
c1 = [0, 1, 0, 1, 1, 0, 0, 1]
31+
n1 = 7
32+
print(sol.prisonAfterNDays(c1, n1), [0, 0, 1, 1, 0, 0, 0, 0])
33+
34+
c2 = [1, 0, 0, 1, 0, 0, 1, 0]
35+
n2 = 1000000000
36+
print(sol.prisonAfterNDays(c2, n2))
37+
38+
c3 = [1, 0, 0, 1, 0, 0, 0, 1]
39+
n3 = 826
40+
print(sol.prisonAfterNDays(c3, n3), [0, 1, 1, 0, 1, 1, 1, 0])

0 commit comments

Comments
 (0)