포스트

210. K번째수


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import Foundation

func solution(_ array:[Int], _ commands:[[Int]]) -> [Int] {
    
    var answer : [Int] = []
    var arr : [Int] = []
    
    for i in 0..<commands.count {
        arr = array[(commands[i][0]-1)...(commands[i][1]-1)].sorted()
        answer.append(arr[commands[i][2]-1])
    }
       
    
    return answer
}

새로운 배열을 하나 만들어서 commands 배열 조건에 맞는 숫자 범위로 해서 값을 넣었다. commands안에 있는 번째는 1을 처음부터 하기에, 우리가 사용하는 인덱스의 개념과 달라 -1을 해주었다.

이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.