๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm/LeetCode

๐Ÿ’ฏ Daily LeetCode Challenge Day_03 - Prison Cells After N Days

by HaningYa 2020. 7. 3.
728x90

 

Explore - LeetCode

LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore.

leetcode.com


๋ฌธ์ œ

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 ์ด 1000000000 ์œผ๋กœ ์–ต์ด ๋„˜์–ด๊ฐ€๋Š” ๋ฒ”์œ„์ด๋‹ค.

์ผ๋ฐ˜์ ์œผ๋กœ 1์–ต๋ฒˆ ์‹คํ–‰์ด 1์ดˆ์ •๋„ ๊ฑธ๋ฆฐ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๊ณ 

๋Œ€๋ถ€๋ถ„ ์ฝ”๋”ฉ๋ฌธ์ œ๋Š” 1์ดˆ ์•ˆ์— ์‹คํ–‰๋˜์–ด์•ผ Time Limit ์ด ์•ˆ๊ฑธ๋ฆฐ๋‹ค๊ณ  ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ž˜ํ•˜์‹œ๋Š” ํ˜•์—๊ฒŒ ๋“ค์—ˆ๋‹ค.

N๋ฒˆ for ๋ฌธ ๋Œ๋ฆฌ๋‹ˆ Time Limit ์ด ๊ฑธ๋ฆฐ๋‹ค.

์ด๋†ˆ์„ ์–ด๋–ป๊ฒŒ ์‹œ๊ฐ„์„ ์ค„์—ฌ์•ผ ํ• ๊นŒ ๊ฒ๋‚˜ ์ƒ๊ฐํ•ด๋„ ๋ฐ˜๋ณต๋ฌธ ํ•˜๋‚˜์ธ๋ฐ ๋ญ˜ ๋” ์–ด๋–ป๊ฒŒ ์ค„์ผ๊นŒ ์‹ถ์—ˆ๋‹ค.

์–ด์จ‹๊ฑด ์‹œ๊ฐ„์„ ์ค„์ผ๋ ค๋ฉด N๋ฒˆ ์ „๋ถ€ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ์ง€ ๋ง๋ผ๋Š” ์–˜๊ธฐ์ธ๊ฒƒ ๊ฐ™์•„ N๋ฒˆ์„ ๋‹ค ๋Œ์ง€ ์•Š๊ณ ๋„

๊ฒฐ๊ณผ๊ฐ’์„ ์ฐพ์„ ์ˆ˜ ์žˆ์„ ์ง€ ์ƒ๊ฐํ–ˆ๋‹ค.

์ˆ˜ํ•™์ ์œผ๋กœ ์ˆ˜์‹์ด ์žˆ๋Š”์ง€ ์ƒ๊ฐํ•ด๋ณด์•˜๊ณ  ์–ด๋–ค ํŒจํ„ด์ด ์žˆ๋Š”์ง€๋„ ํ™•์ธํ•ด ๋ณด์•˜๋‹ค.

ํŒจํ„ด์ด ์žˆ์—ˆ๋‹ค.

14๋ฒˆ ๋‹จ์œ„๋กœ ๊ฐ™์€ ํŒจํ„ด์ด ๋ฐ˜๋ณต๋˜๊ณ  ์žˆ์—ˆ๋‹ค.

๊ทธ๋Ÿผ 14๋ฒˆ๊นŒ์ง€๋งŒ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๊ณ  ๊ฒฐ๊ณผ๋Š” 14๋ฅผ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€ ๊ฐ’์„ ์‚ฌ์šฉํ•˜๋ฉด ๋— ๐Ÿ‘

class Solution {
    var arr : [Int] = []
    func prisonAfterNDays(_ cells: [Int], _ N: Int) -> [Int] {
        arr = cells
        var answer : [[Int]] = []
        // print(arr)
        for i in 0..<N{
             if (i > 14){
                break
            }
            answer.append(check())
        }
        // print(answer)
        // for i in answer{
        //     print(i)
        // }
        // print(answer[0])
        if N%14 != 0 {
            return answer[N%14 - 1]
        }else{
            return answer[13]
        }
    }
    func check() -> [Int] {
      
        var tmp : [Int] = arr
        tmp[0] = 0
        tmp[7] = 0
         
        for i in 1...6 {
            if arr[i-1] == 0 && arr[i+1] == 0 {
                tmp[i] = 1
            }else if arr[i-1] == 1 && arr[i+1] == 1 {
              tmp[i] = 1
                
            }else{
                tmp[i] = 0
            }
        }
        arr = tmp
        // print(arr)
        return arr
    }
    
}
728x90

๋Œ“๊ธ€