[프로그래머스] 스택/큐 - 프린터

김준영·2023년 3월 20일
1

코딩테스트

목록 보기
14/22

풀이

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

        PriorityQueue<Integer> queue = new PriorityQueue<>(Collections.reverseOrder());
        for(int p : priorities){
            queue.add(p);
        }
        while(!queue.isEmpty()){
            for(int i = 0; i < priorities.length; i++){
                if(queue.peek() == priorities[i]){
                    queue.poll();
                    answer++;
                    if(location == i) return answer;
                }
            }
        }

        return answer;
    }
}
  1. 우선 순위 큐를 사용하고 내림 차 순으로 등록.
  2. 인쇄 목록을 큐에 넣어준다.
  3. 큐가 빌때까지 반복.
💡 ex → priorities{ 1, 1, 9, 1, 1, 1} / queue{9, 1, 1, 1, 1, 1}
  1. 큐에 처음 값과 priorities 값을 비교하여 같으면 큐에서 값을 뽑고 answer++를 해준다.
  2. 만약 값과 함께 위치까지 같으면 answer를 리턴해준다.
profile
ㅎㅎ

0개의 댓글