알고리즘/프로그래머스
[프로그래머스][스칼라] K번째수
hs_seo
2019. 5. 7. 22:20
https://programmers.co.kr/learn/courses/30/lessons/42748?language=scala
알고리즘 연습 - K번째수 | 프로그래머스
실행 결과가 여기에 표시됩니다.
programmers.co.kr
object Solution01 extends App {
var array = Vector(1, 5, 2, 6, 3, 7, 4)
var commands = Vector(Vector(2, 5, 3), Vector(4, 4, 1), Vector(1, 7, 3))
def solution(array: Vector[Int], commands: Vector[Vector[Int]]): Vector[Int] = {
def innerSol(array: Vector[Int], command: Vector[Int]): Int = {
var from = command(0) - 1
var to = command(1)
var index = command(2) - 1
return array.slice(from, to).sortWith(_ < _)(index)
}
return commands.map(innerSol(array, _)).collect({ case i: Int => i })
}
solution(array, commands)
}
반응형