[ 프로그래머스 ] #42587 : 프로세스 - JAVA

🔗 프로세스

import java.util.*;
import java.util.Collections;
class Solution {
    public int solution(int[] priorities, int location) {

        // index를 담는 큐
        Queue<int[]> queue=new LinkedList<>();
        for(int i=0;i<priorities.length;i++){//priorities 저장
            queue.offer(new int []{priorities[i],i} );// {우선순위, index}
        }
        // poll 할때 큰 순으로 나오도록
        Queue<Integer> maxQueue=new PriorityQueue<>(Collections.reverseOrder());
        for(int p: priorities){//우선순위 저장
            maxQueue.offer(p);
        }
        // 몇 번째로 실행되는 지
        int count =0;

        // 큐에 값이 없을 때까지 반복
        while(!queue.isEmpty()){
            // 해당 i 프로세스의 우선순위 ({우선순위, 인덱스})
            int []priority=queue.poll();

            // 우선순위 최댓값
            int maxPriority=maxQueue.peek();

            // 우선순위가 더 큰게 큐에 아직 있다면
            if(maxPriority>priority[0]){
                // 뒤에 넣기
                queue.offer(priority);
                continue;
            }
            // 실행될 것이고 location 과 같다면
            else if(priority[1]==location){
                // 실행
                count ++;
                return count;
            }
            // location 과 같지 않고 단순히 실행 된다면
            else{
                // max queue 업데이트
                maxQueue.poll();
                // 실행
                count++;
            }
        }
        return count;

    }
}