새소식

인기 검색어

프로그래머스/Lv.1

[Swift] 달리기 경주

  • -
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
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.