본문 바로가기

Study/알고리즘

[프로그래머스] 42748. K번째 수 (swift)

 

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

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

programmers.co.kr

 

풀이

주어진 요구사항 그대로 구현하면 되는 쉬운 문제이다.

map을 사용해서 다시 풀어보기!

 

 

코드

import Foundation

func solution(_ array:[Int], _ commands:[[Int]]) -> [Int] {
    var ans: Array<Int> = []
    for i in commands {
        var temp: Array<Int> = []
        for j in i[0]...i[1] {
            temp.append(array[j-1])
        }
        temp.sort()
        ans.append(temp[i[2]-1])
    }
    return ans
}