|
| 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 | +
|
0 commit comments