K번째수

WooBuntu·2020년 7월 26일
0

K번째수

2020.07.26

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];
    이렇게 한 줄로도 요약이 가능한데 내가 한 것처럼 단계를 보여주는 것이 나은지 이게 나은지는 잘 모르겠다.

2020.09.12

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];
  });
}

0개의 댓글