728x90

 

 

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

풀이 - 나의 생각

1. 맨 앞에 있는 프로세스 꺼내 

 

2. 그 프로세스보다 우선 순위 높은지(priorities[arr.length - cnt]) 확인

 

3-1. YES -> 다시 queue에 넣기

 

3-2. NO == 현재 프로세스가 가장 높은 우선순위임( priorities[arr.length - cnt] )


cnt++; (실행된 프로세스 개수)


-> 이 프로세스가 내가 알고 싶었던 프로세스야?(location == 0)
            Yes -> break;

 

위의 과정만 진행하면 답이 나오는 문제였다.

 

이 걸 위해서는 우선순위가 정렬되어 있는 배열이 필요했다.

 

그래서 주어진 배열( priorities)을

 

순서 그대로 Queue에 넣고

 

Arrays.sort( priorities)로 정렬 후

 

뒤에서 부터 값을 확인했다.

 

코드

 

import java.util.*;

class Solution {
    public int solution(int[] priorities, int location) {
        int answer = 0;
        Queue<Integer> q = new ArrayDeque<>();
        
        for(int i : priorities){
            q.offer(i);
        }
        Arrays.sort(priorities);
        int len = priorities.length-1;        
        /* 맨 앞에 있는 프로세스 꺼내
        그 프로세스보다 우선 순위 높은지(priorities[max]) 확인
        YES -> 다시 queue에 넣기
        NO == 현재 프로세스가 가장 높은 우선순위임(priorities[max])
            -> answer++; (프로세스 실행 순서)
            -> 이 프로세스가 내가 알고 싶었던 프로세스야?(location == 0)
            Yes -> break;
        */
        while(!q.isEmpty()){
            int now = q.poll();
            if(now < priorities[len - answer]){
                q.offer(now);
            }else{
                answer++;
                if(location == 0){
                    break;
                }
            }
            location = location-1<0?q.size()-1:location-1;
        }
        return answer;
    }
}

 

 

 

 

 

 

 

다음에 또 봐요

 

+ Recent posts