-
[Level1] K번째 수Algorithm/Programmers 2021. 2. 15. 05:25
채점 결과
정확성: 85.7
합계: 85.7 / 100.0
function solution(array, commands) { let answer = []; commands.forEach(ele => { let tmp = []; tmp = array.slice(ele[0] - 1, ele[1]).sort(); answer.push(tmp[ele[2] - 1]); }) return answer; }
한개 테스트를 통과하지 못했다.
문제는 바로 정렬이 문제였다.
function solution(array, commands) { let answer = []; commands.forEach(ele => { let tmp = []; tmp = array.slice(ele[0] - 1, ele[1]).sort((a, b) => a - b); answer.push(tmp[ele[2] - 1]); }) return answer; }
기억하자 JavaScript 숫자 정렬은 항상 신경써야 한다는 것을ㅎㅎ
let numbers = [4, 2, 5, 1, 3]; numbers.sort((a, b) => a - b); console.log(numbers); // [1, 2, 3, 4, 5]
developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
Array.prototype.sort() - JavaScript | MDN
The sort() method sorts the elements of an array in place and returns the sorted array. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values. The time and space com
developer.mozilla.org
'Algorithm > Programmers' 카테고리의 다른 글
[Level1] 두 개 뽑아서 더하기 (0) 2021.02.08 [Level1] 완주하지 못한 선수 (0) 2021.02.08