프로그래머스 K번째 수 자바스크립트

Chaeyeon Lee·2023년 6월 16일
0


https://school.programmers.co.kr/learn/courses/30/lessons/42748

🔅 1. 아이디어

commands를 순회하면서 array를 slice로 잘라야지.
splice가 아닌 slice를 고른 이유는
1. 주어진 commands가 n번째에서 k개를 자르라는 게 아니라, n번째에서 k번째까지 자르라는 것이기 때문
2. 원본 배열이 commands의 length만큼 다시 쓰여야 하는데, slice는 원본배열에 영향을 미치지 않으므로.
비교적 쉬운 문제였던 것 같다.

🧑‍💻 2. 내 코드

function solution(array, commands) {
    let ans=[];
    commands.map(e=>{
        let temp=array.slice(e[0]-1,e[1]);
        temp.sort((a,b)=>a-b);
        ans.push(temp[e[2]-1]);
    });
    return ans;
}
profile
프론트엔드 개발자 지망생

0개의 댓글