728x90
문제 풀이
players는 고유한 값으로 크게 분기가 나뉘지 않아서 바로 브루트포스로 풀어보았으나, 시간 초과가 나와서 해시로 도전했어요.
players를 Dictionary에 id(등수), 이름으로 나눠서 넣고, callings에서 이름을 받아 Dictionary와 players를 갱신해서 통과했습니다.
정답 코드
더보기
더보기
func solution(_ players:[String], _ callings:[String]) -> [String] {
var result: Dictionary<String, Int> = Dictionary<String, Int>()
var playerList = players
players.enumerated().map {
result.updateValue($0 + 1, forKey: $1)
}
callings.map { call in
guard let idx = result[call] else { return }
result.updateValue(idx - 1, forKey: call)
playerList.swapAt(idx - 1, idx - 2)
result.updateValue(idx, forKey: playerList[idx - 1])
}
return playerList
}
728x90
'Algorithm' 카테고리의 다른 글
SwiftUI: 덧칠하기 (1) | 2023.12.17 |
---|---|
SwiftUI: 추억 점수 (0) | 2023.12.17 |
SwiftUI: 성격 유형 검사하기 (1) | 2023.12.17 |
SwiftUI: 개인정보 수집 유효기간 (0) | 2023.08.30 |
SwiftUI: 공원 산책 (0) | 2023.08.29 |