상세 컨텐츠

본문 제목

[프로그래머스] 프로세스

💯ProblemSolving/문제 풀이-Java

by :부셔져버린개발자 2025. 2. 3. 08:51

본문

       문제 요약        

- 문제

https://school.programmers.co.kr/learn/courses/30/lessons/42587

 

 

         아이디어        

우선순위 큐 

 

2가지 이용해서 현재 우선순위 가장 높은 것 빠르게 찾기 

         소스코드        

 

import java.util.*;
import java.io.*;
class Solution {
    public int solution(int[] priorities, int location) {
        int answer = 0;
        PriorityQueue<Item> pq = new PriorityQueue<>();
        Deque<Item> queue = new ArrayDeque<>();
        for(int i = 0; i < priorities.length; i++) {
            Item o = new Item(i, priorities[i]);
            queue.add(o);
            pq.add(o);
        }
        
        while(true){
            Item current = queue.pollFirst();
            if(current.value < pq.peek().value) { // 우선순위가장 높은거보다 작다면
                queue.addLast(current); // 다시 큐에 넣음
            } else {
                answer += 1; // 프린트할 거니까
                if(current.i == location)break;
                pq.poll(); // 다음 우선순위 구하기 위해 진행
            }
        }
        return answer;
    }
    static class Item implements Comparable<Item>{
        int i; // location
        int value; 
        Item(int i , int value) {
            this.i = i;
            this.value = value;
        }
        @Override
        public int compareTo(Item o){
            return o.value - this.value ;
        }
    }
}

 

728x90

관련글 더보기