210. K번째수

Harold's velog·2024년 3월 9일

CodingTest (Class)

목록 보기
46/52


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을 해주었다.

profile
데일리 정리, 하루에 최소 하나의 글은 적도록 하자.

0개의 댓글