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

조예빈·2024년 7월 9일
0

Coding Test

목록 보기
44/138

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

import java.util.*;

class Solution {
    public int solution(int[] priorities, int location) {
        PriorityQueue<Integer> queue = new PriorityQueue<>(Collections.reverseOrder()); //숫자가 큰 것이 더 높은 우선순위를 갖도록 정렬
        int length = priorities.length;
        int cnt = 0;
        for(int i=0;i<length;i++){
            queue.add(priorities[i]);
        }
        while(!queue.isEmpty()){
            for(int i=0;i<length;i++){
                if(queue.peek() == priorities[i]){ //큐의 가장 먼저 나오는 값이 배열에서의 순서와 같으면(이 때 같지 않으면 제거하지 X)
                    queue.poll(); //큐에서 제거
                    cnt++;
                    if(i == location){
                        return cnt++;
                    }
                }
            }
        }
        return cnt;
    }
}

profile
컴퓨터가 이해하는 코드는 바보도 작성할 수 있다. 사람이 이해하도록 작성하는 프로그래머가 진정한 실력자다. -마틴 파울러

0개의 댓글