본문 바로가기

2.알고리즘/프로그래머스

[JS] 프로그래머스 K번째 수

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

제출코드

 

function solution(array, commands) {
    const answer = [];
    for (let i = 0; i< commands.length; i++){
       answer.push(array.slice(commands[i][0]-1,commands[i][1]).sort((a, b) => a - b)[commands[i][2]-1])
    }
    return answer;
}

 


 

 

 

반복문만 돌리면 된다고 생각이 변경되어서 리팩토링 하였습니다

 

 

function solution(array, commands) {
    const answer = [];
    commands.forEach((e,i) => answer.push(array.slice(commands[i][0]-1,commands[i][1]).sort((a, b) => a - b)[commands[i][2]-1]))

    return answer;
}
반응형

'2.알고리즘 > 프로그래머스' 카테고리의 다른 글

[JS]신규 아이디 추천  (0) 2022.08.24
[JS]소수 만들기  (0) 2022.08.19
[JS] 프로래머스 숫자 문자열과 영단어  (0) 2022.08.05
빅오 표기법(Big-o Notation)  (0) 2022.04.14
[JS]음양 더하기  (0) 2020.02.04