본문으로 바로가기

[Swift]프로그래머스 K번째 수

category 코딩테스트/Swift 2021. 9. 21. 22:48
import Foundation

func solution(_ array:[Int], _ commands:[[Int]]) -> [Int] {
    
    var arr = array
    var arr2 = [Int]()
    var result = [Int]()
    
    for i in 0..<commands.count {
        for _ in commands[i][0]...commands[i][1] {
            let a = arr.remove(at: commands[i][0] - 1)
            arr2.append(a)
        }
        arr2 = arr2.sorted()
        result.append(arr2[commands[i][2] - 1])
        arr = array
        arr2 = [Int]()
    }

    return result
}