티스토리 뷰

문제
코딩테스트 연습 - K번째수 | 프로그래머스 (programmers.co.kr)
코딩테스트 연습 - K번째수
[1, 5, 2, 6, 3, 7, 4] [[2, 5, 3], [4, 4, 1], [1, 7, 3]] [5, 6, 3]
programmers.co.kr

코드
function solution(array, commands) {
let answer = [];
for (let command of commands) {
answer.push(array.slice(command[0]-1, command[1]).sort((a, b) => a - b)[command[2]-1]);
}
return answer;
}
solution( [1, 5, 2, 6, 3, 7, 4], [ [2, 5, 3], [4, 4, 1], [1, 7, 3] ] ); // [5, 6, 3]
다른 풀이
function solution(array, commands) {
return commands.map(v => {
return array.slice(v[0] - 1, v[1]).sort((a, b) => a - b).slice(v[2] - 1, v[2])[0];
});
}
solution( [1, 5, 2, 6, 3, 7, 4], [ [2, 5, 3], [4, 4, 1], [1, 7, 3] ] ); // [5, 6, 3]
참고
JavaScript / array.map — DevDocs
'ALGORITHM > Programmers + α' 카테고리의 다른 글
| [프로그래머스] lv1. 체육복 (0) | 2022.06.16 |
|---|---|
| [프로그래머스] lv1. 모의고사 (0) | 2022.06.14 |
| [프로그래머스] lv1. 소수 만들기 (0) | 2022.06.07 |
| [프로그래머스] lv1. 없는 숫자 더하기 (0) | 2022.06.07 |
| [프로그래머스] lv1. 키패드 누르기 (0) | 2022.06.07 |
댓글