티스토리 뷰

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

 

코딩테스트 연습 - 크레인 인형뽑기 게임

[[0,0,0,0,0],[0,0,1,0,3],[0,2,5,0,1],[4,2,4,4,2],[3,5,1,3,1]] [1,5,3,5,1,2,1,4] 4

programmers.co.kr

나의 풀이

import Foundation

func solution(_ board:[[Int]], _ moves:[Int]) -> Int {
    
    var board = board
    var arr: [Int] = []
    var removeCount: Int = 0
    
    for i in moves {
        for j in 0..<board.count {
            let target = board[j][i-1]
            if target != 0 {
                board[j][i-1] = 0
                if arr.last == target {
                    arr.removeLast()
                    removeCount += 1
                } else {
                    arr.append(target)
                }
                break
            }
        }
    }
    
    return removeCount * 2
}

 

댓글
공지사항