๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“– Problem Solution/Programmers

Swift ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค K๋ฒˆ์งธ ์ˆ˜

by Fomagran ๐Ÿ’ป 2020. 2. 7.
728x90
๋ฐ˜์‘ํ˜•

Youtube ํ’€์ด์˜์ƒ

 


Problem

 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - K๋ฒˆ์งธ์ˆ˜

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

programmers.co.kr


Source Code

 

func solution(_ array:[Int], _ commands:[[Int]]) -> [Int] {
    
    var answer:[Int] = []
    
    for command in commands {
        let i = command[0] - 1
        let j = command[1] - 1
        let k = command[2] - 1
        
        let number = Array(array[i...j]).sorted()[k]
        answer.append(number)
    }
    
    return answer
}
func solution(_ array:[Int], _ commands:[[Int]]) -> [Int] {
    
    var answer:[Int] = []
    
    commands.forEach {
        answer.append(Array(array[$0[0] - 1...$0[1]-1]).sorted()[$0[2]-1])
    }
    
    return answer
}
728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€