const sortHelper = (a, b) => {
return a - b;
};
const solution = (array, commands) => {
const result = commands.map(([start, end, index]) => {
const slicedArray = array.slice(start - 1, end);
const sortedArray = slicedArray.sort(sortHelper);
return slicedArray[index - 1];
});
return result;
};
비교적 쉬운 문제였다.
다만, map안에서
const result = array.slice(start-1, end).sort(sortHelper)[index-1];
이렇게 한 줄로도 요약이 가능한데 내가 한 것처럼 단계를 보여주는 것이 나은지 이게 나은지는 잘 모르겠다.
function solution(array, commands) {
return commands.map(([start, end, nth]) => {
const copied = array.slice(start - 1, end).sort((a, b) => a - b);
return copied[nth - 1];
});
}