Algorithm/Programmers

[알고리즘/프로그래머스/고득점 Kit] 프로세스(JS)

개발자 김비숑 2023. 9. 9. 20:11
반응형

문제


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])
        }
    }

}

 

반응형