반응형
문제
https://github.com/tony9402/baekjoon
GitHub - tony9402/baekjoon: 코딩테스트 대비 문제집(Baekjoon Online Judge)
코딩테스트 대비 문제집(Baekjoon Online Judge). Contribute to tony9402/baekjoon development by creating an account on GitHub.
github.com
풀이
1. [순서, 우선순위] 큐를 만든다.
2. tasks를 앞에서부터 하나씩 꺼내서 확인한다. 우선순위가 가장 높은 경우 실행 순서인 order를 1 증가시킨다. 이때 만약 순서(idx)가 찾고자 하는 location이라면 order를 반환한다. 아니라면 다시 우선순위 최댓값을 남은 큐에서 찾는다.
3. 만약 우선순위가 최댓값이 아니라면 꺼냈던 task를 다시 push한다.
function solution(priorities, location) {
let maxVal = Math.max(...priorities)
const tasks = Array.from({length: priorities.length}, (val, idx) => [idx, priorities[idx]])
let order = 0
while (tasks.length > 0) {
const [idx, val] = tasks.shift()
// 가장 우선순위가 높다면
if (maxVal <= val) {
order += 1
if (idx === location) return order
maxVal = Math.max(...tasks.map(([_, val]) => val))
} else {
tasks.push([idx, val])
}
}
}
반응형
'Algorithm > Programmers' 카테고리의 다른 글
[알고리즘/프로그래머스/고득점Kit] 정렬(JS)(K번째수, 가장 큰 수, H-Index) (0) | 2023.09.14 |
---|---|
[알고리즘/프로그래머스/고득점Kit] 해시(JS)브라운 먹고싶다/..(폰켓몬, 완주하지 못한 선수, 의상, 전화번호 목록, 베스트 앨범) (0) | 2023.09.13 |
[알고리즘/프로그래머스/고득점 Kit] 올바른 괄호(JS) (0) | 2023.09.09 |
[알고리즘/프로그래머스/고득점Kit] 기능개발(JS) (1) | 2023.09.09 |
[알고리즘/프로그래머스/카카오] 메뉴 리뉴얼(JS) (0) | 2023.08.31 |