STUDYING/Algorithm

[Programmers] K번째 수

EOZIN 2021. 9. 27. 00:33
728x90

https://programmers.co.kr/learn/courses/30/lessons/42748

 

코딩테스트 연습 - K번째수

[1, 5, 2, 6, 3, 7, 4] [[2, 5, 3], [4, 4, 1], [1, 7, 3]] [5, 6, 3]

programmers.co.kr

import Foundation

func solution(_ array:[Int], _ commands:[[Int]]) -> [Int] {
    var res = [Int]()
    
    for i in 0..<commands.count {
        let start = commands[i][0] - 1
        let end = commands[i][1] - 1
        let num = commands[i][2] - 1
        
        var tmp = [Int]()
        for j in start...end {
            tmp.append(array[j])
        }
        
        tmp.sort()
        res.append(tmp[num])
    }
    return res
}