알고리즘
[프로그래머스 알고리즘] 2중반복문
고래강이
2023. 6. 22. 09:57
K번째 수
function solution(array, commands) {
let answer = []
for(const i of commands) {
let newArr = []
for (let j = i[0] - 1; j < i[1]; j++) {
newArr.push(array[j])
}
answer.push(newArr.sort((a, b) => a-b)[i[2] - 1])
}
return answer
}
다른사람 답
function solution(array, commands) {
return commands.map(command => {
const [sPosition, ePosition, position] = command
const newArray = array
.filter((value, fIndex) => fIndex >= sPosition - 1 && fIndex <= ePosition - 1)
.sort((a,b) => a - b)
return newArray[position - 1]
})
}