Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] (Swift) Kakao ํฌ๋ ˆ์ธ ์ธํ˜•๋ฝ‘๊ธฐ ๊ฒŒ์ž„

๊ฐ์ž ๐Ÿฅ” 2022. 4. 28. 16:09
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ๋งํฌ

https://programmers.co.kr/learn/courses/30/lessons/64061

 

๋ฌธ์ œ ํ’€์ด

  • board ์—์„œ ๋ฝ‘๋Š” ์ธํ˜• pickDoll
  • pickDoll์„ ๋„ฃ๋Š” ๋ฐ”๊ตฌ๋‹ˆ๋Š” baguni
  • ์ด๋™ํ•˜๋ ค๋Š” ๊ฒฝ๋กœ moves

  • ๋„ค์ผ ์œ„์—์žˆ๋Š” ๊ฒƒ๋“ค์„ ํƒ์ƒ‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” baguni[i][move-1]์„ ํ•ด์•ผ ์›ํ•˜๋Š” ์œ„์น˜์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ณ , ์ด๋•Œ ์ด ๊ฐ’์ด 0์ด๋ผ๋ฉด i๋ฅผ ๋ฐ”๊ฟ”์ฃผ๋ฉฐ ๋ฐ‘์—์žˆ๋Š” ๊ฐ’์„ ํƒ์ƒ‰ํ•ด์•ผํ•œ๋‹ค.
  • baguni์— ๋‘ ๊ฐœ์ด์ƒ ๊ฒน์น˜๋Š” ์ธํ˜•์ด ๋‚˜์˜ค๋ฉด, result ์—๋‹ค๊ฐ€ ๊ทธ ๊ฐœ์ˆ˜๋ฅผ + ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. 

 

๋ฌธ์ œ ํ’€๋‹ค๊ฐ€ ๋ฐœ๊ฒฌํ•œ ์ด์Šˆ์‚ฌํ•ญ

if baguni.count != 0 && pickDoll == baguni[baguni.count-1] { } // ์ •๋‹ต
if pickDoll == baguni[baguni.count-1] && baguni.count != 0 { } // ์—๋Ÿฌ

์œ„ ๋‘๊ฐœ์˜ ์ฝ”๋“œ๋Š” ๊ฐ™์€ ์กฐ๊ฑด์„ ๊ฐ€์ง„ if ๋ฌธ์ด๋‹ค. ํ•˜์ง€๋งŒ ๋ฐ‘์—์žˆ๋Š” ์ฝ”๋“œ๋Š” ์—๋Ÿฌ๊ฐ€ ๋‚œ๋‹ค. if ๋ฌธ์—์„œ ๋‘ ๊ฐ€์ง€ ์ด์ƒ์˜ ์กฐ๊ฑด์„ ๋„ฃ์„ ๋•Œ, ์ˆœ์„œ๊ฐ€ ์ค‘์š”ํ•˜์ง€ ์•Š์•˜๋˜ ๊ฒƒ ๊ฐ™์€๋ฐ, ๊ทธ๋ฆฌ๊ณ  ์–ด์ฐจํ”ผ And ์—ฐ์‚ฐ์ž๋ผ ์ˆœ์„œ๊ฐ€ ์ƒ๊ด€์—†์„๊ฒƒ ๊ฐ™์€๋ฐ ์•„๋ž˜์™€ ๊ฐ™์€ ์—๋Ÿฌ๊ฐ€ ๋‚œ๋‹ค.

signal: illegal instruction (core dumped)

ํ•ด๋‹น ์—๋Ÿฌ๋Š” ๋…ผ๋ฆฌ์ ์ธ ์˜ค๋ฅ˜๊ฐ€ ์žˆ๊ฑฐ๋‚˜, index of range ์—๋Ÿฌ๊ฐ€ ๋‚ฌ๊ฑฐ๋‚˜, nil ๊ฐ’์ด ์ƒ๊ฒจ์„œ ๊ทธ๋ ‡๋‹ค๊ณ  ํ•˜๋Š”๋ฐ,, ๋‚ด ์ฝ”๋“œ์—์„œ๋Š” ์ •๋ง ์ด์œ ๋ฅผ ๋ชจ๋ฅด๊ฒ ๋‹ค.  ๊ทธ๋ž˜๋„ ์–ด์จ‹๋“ , ๊ฐ™์ด ์Šคํ„ฐ๋””ํ•˜๋Š” ์Šคํ„ฐ๋””์›์˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋‹ˆ ๋‚˜์™€ If ๋ฌธ ์ˆœ์„œ๊ฐ€ ๋‹ค๋ฅด๊ธธ๋ž˜ ์ด๊ฒƒ๋งŒ ๋ฐ”๊ฟจ๋”๋‹ˆ ์ •๋‹ต์„ ๋งž์ถœ ์ˆ˜ ์žˆ์—ˆ๋‹ค. 

 

์ „์ฒด ์ฝ”๋“œ

func solution(_ board:[[Int]], _ moves:[Int]) -> Int {
    
    var result = 0
    var baguni: [Int] = []
    var tempBoard = board
    
    for move in moves {
        for i in 0..<board.count {
            let pickDoll = tempBoard[i][move-1]
            
            if pickDoll == 0 {
                continue
            } else {
                if baguni.count != 0 && pickDoll == baguni[baguni.count-1] {
                    baguni.removeLast()
                    result += 2
                    tempBoard[i][move-1] = 0
                } else {
                    baguni.append(pickDoll)
                    tempBoard[i][move-1] = 0
                }
                break
            }
        }
    }
    return result
}
๋ฐ˜์‘ํ˜•