iOS 개발 공부

[Swift] 달리기 경주 본문

프로그래머스/Lv.1

[Swift] 달리기 경주

물복딱복준복 2023. 4. 25. 15:57
import Foundation

func solution(_ players:[String], _ callings:[String]) -> [String] {
    var players = players
    var rank: [String: Int] = Dictionary(uniqueKeysWithValues: zip(players, 0..<players.count))
    
    callings.forEach { calling in
        let index = rank[calling]!
        let overtakenPlayer = players[index - 1]
        players.swapAt(index, index-1)
        rank[calling]! -= 1
        rank[overtakenPlayer]! += 1
    }
    
    return players
}

 

'프로그래머스 > Lv.1' 카테고리의 다른 글

[Swift] 바탕화면 정리  (0) 2023.04.25
[Swift] 공원 산책  (0) 2023.04.25
[Swift] 덧칠하기  (0) 2023.04.15
[Swift] 카드뭉치  (0) 2023.04.14
[Swift] 추억점수  (0) 2023.04.14