[Swift 알고리즘] 크레인 인형뽑기 게임

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

import Foundation

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

좋은 웹페이지 즐겨찾기