문제 풀이/프로그래머스

[프로그래머스] 프로세스(JAVA)

27200 2024. 4. 25. 20:20

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


풀이

import java.util.*;
class Solution {
    public int solution(int[] priorities, int location) {
        int[] arr = priorities.clone();
        arr[location] += 10;
        Queue<Integer> q = new LinkedList<>();
        for(int i = 0; i < arr.length; i++){
            q.add(arr[i]);
        }
        Arrays.sort(priorities);
        
        int answer = 0;
        int idx = priorities.length -1;
        int prior = priorities[idx];
        while(!q.isEmpty()){
            int temp = q.poll();
            if(temp > 10 && temp - 10 == prior){
                return answer + 1;
            }else if(temp == prior){
                answer++;
                idx--;
                prior = priorities[idx];
            }else{
                q.add(temp);
            }
        }

        return answer;
    }
}

 

location의 위치에 대한 해결이 중요한 문제였다.

 

location의 경우 특별한 마킹을 해줘야 함이 분명한데도 그것을 처리하기 위해 다른 자료구조를 쓰는 것은 너무 큰 손해같았다.

따라서 +10을 해줘서(우선순위는 1~9까지이므로 확실히 구분됨) 구분을 하고, 이후에 큐에서 꺼낸 값이 10보다 크다면을 따로 처리해줬다.