[프로그래머스] K번째수(Kotlin)
풀이
class Solution {
fun solution(array: IntArray, commands: Array<IntArray>): IntArray {
var answer = intArrayOf()
for(cmd in commands){
val i = cmd[0]-1
val j = cmd[1]-1
val k = cmd[2]-1
val subArray = array.copyOfRange(i,j+1).sorted()
answer += subArray[k]
}
return answer
}
}
class Solution {
fun solution(array: IntArray, commands: Array<IntArray>) =
commands.map{ it ->
val i = it[0]-1
val j = it[1]-1
val k= it[2]-1
array.slice(i..j).toList().sorted().get(k)
}.toIntArray()
}
📌참고자료