티스토리 뷰
https://school.programmers.co.kr/learn/courses/30/lessons/42748
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
이중 for문
function solution(array, commands) {
const answer = []
for(let i = 0; i < commands.length; i ++) {
const temp = []
const start = commands[i][0] - 1
const end = commands[i][1] - 1
const pick = commands[i][2] - 1
for(let j = start; j <= end; j ++) {
temp.push(array[j])
}
answer.push(temp.sort((a, b) => a - b)[pick])
}
return answer
}

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

다른 사람의 풀이
map(), 구조 분해 할당
function solution(array, commands) {
const newArray = commands.map(command => {
const [start, end, pick] = command
return array.slice(start - 1, end).sort((a, b) => a - b)[pick - 1]
})
return newArray
}

'알고리즘 문제풀이 > 프로그래머스' 카테고리의 다른 글
| [프로그래머스] 1단계 - 모의고사 (javascript) (0) | 2022.08.02 |
|---|---|
| [프로그래머스] 1단계 - 숫자 문자열과 영단어 (javascript) (0) | 2022.07.28 |
| [프로그래머스] 1단계 - 없는 숫자 더하기 (javascript) (0) | 2022.07.26 |
| [프로그래머스] 1단계 - 두 개 뽑아서 더하기 (javascript) (0) | 2022.07.25 |
| [프로그래머스] 1단계 - 서울에서 김서방 찾기 (javascript) (0) | 2022.07.18 |
댓글
