programmers - K번째 수 with JS

@p-iknow 🎹 · September 04, 2019

Problem
What I learned
Solve
내 풀이
다른 사람 풀이

Problem

k번째 수

What I learned

array sort 유니코드 방식의 비교

  • array의 sort 함수는 애초에 문자열 정렬을 위해 탄생했기 때문에, compare 함수를 인자로 전달해야 한다.

  • 링크

Solve

내 풀이

const commandToK = (arr, command) => {
  let [i, j, k] = command.map(item => item - 1);
  j += 1;
  return arr.slice(i, j).sort((a, b) => a - b)[k];
};

const solution = (arr, commands) => {
  const answer = commands.map(command => {
    return commandToK(arr, command);
  });
  return answer;
};

다른 사람 풀이

function solution(array, commands) {
    return commands.map(([from, to, k]) => {
        return array.slice(from - 1, to).sort((x, y) => x > y)[k-1];
    });
} 

 const [from, to, k] = command 의 array destructuring 부분이 인상적임

@p-iknow 🎹
많은 것을 이해하고 싶습니다. 더 이해하기 위해 노력합니다.