문제
https://school.programmers.co.kr/learn/courses/30/lessons/42587
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
코드
function solution(priorities, location) {
let maxPriority = Math.max(...priorities);
let order = 1;
while(priorities.length!==0){
const first = priorities.splice(0,1)[0]; // priorities.shift();
if(first!==maxPriority){
priorities.push(first);
}else{
if(location===0){
return order;
}
order+=1;
maxPriority = Math.max(...priorities);
}
location = location===0 ? priorities.length-1:location-1;
}
}
정리
배열 최댓값, 최솟값 구하기
console.log(Math.max(1,2,3,4,5)) //5
console.log(Math.min(1,2,3,4,5)) //1
const arr = [1,2,3,4,5];
console.log(Math.max(...arr)) //5
console.log(Math.min(...arr)) //1
배열 변수가 주어졌을 경우 spread연산자를 활용하여 배열의 최댓값과 최솟값을 구할 수 있다.
배열의 첫번째 요소 제거하기 shift()
const arr = [1,2,3,4,5];
const first = arr.shift();
console.log(first); // 1
console.log(arr); // [2,3,4,5]
shift()함수를 사용하면 배열의 첫번째 요소를 제거하고 그 제거된 값을 반환한다.
원본 배열을 변경한다.
참조
'Programmers' 카테고리의 다른 글
| [프로그래머스] 타겟 넘버 (JS) - DFS (0) | 2023.11.17 |
|---|---|
| [프로그래머스] 카펫 (JS) - 완전탐색, 이차 방정식 (0) | 2023.11.15 |
| [프로그래머스] 체육복 (JS) - 배열에서 원소 삭제하기, splice, filter (0) | 2023.11.13 |
| [프로그래머스] 올바른 괄호 (JS) (0) | 2023.11.12 |
| [프로그래머스] 기능 개발 (JS) (0) | 2023.11.12 |