본문 바로가기
코테/프로그래머스

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

by 리드맥 2022. 1. 12.
func solution(_ array:[Int], _ commands:[[Int]]) -> [Int] {
    
    var result:[Int] = []
    
    for i in 0...commands.count - 1 {
        var arr:[Int] = []
        
        //2,5
        for j in commands[i][0] - 1...commands[i][1] - 1 {
            arr.append(array[j])
            arr.sort{$0<$1}
        }
//        print(arr)
        var three = commands[i][2]
        result.append(arr[three - 1])
    }
    
    return result
}

sort 고차함수 사용,  중첩 for문 사용

댓글