1 minute read



문제 풀이


이 문제는 백준의 프린터 큐 문제와 요구사항이 동일하다. 여기서도 동일하게 를 잘 활용할 수 있다면 쉽게 풀 수 있다.


아이디어 도출

아이디어는 프린터 큐 문제 풀이와 동일하게 접근했기에 해당 문제 풀이를 참고하자. 문제 풀이를 위해 작성한 코드는 아래와 같다.



작성 코드


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import java.util.*;

class Solution {
    public int solution(int[] priorities, int location) {
        int answer = 0;
        LinkedList<int[]> q = new LinkedList<>();

        for(int i=0; i<priorities.length; i++) {
            q.offer(new int[]{i, priorities[i]});
        }

        while(!q.isEmpty()) {
            int[] first = q.poll();
            boolean isMax = true;
            for(int i=0; i<q.size(); i++) {
                // 꺼낸 원소가 나머지 원소들보다 중요도가 낮을 경우
                // 중요도가 낮을 때까지 꺼낸 원소들을 큐의 마지막으로 삽입한다.
                if(first[1] < q.get(i)[1]) {
                    q.offer(first);
                    for(int j=0; j<i; j++) {
                        q.offer(q.poll());
                    }
                    isMax = false;
                    break;
                }
            }
            // 꺼낸 원소와 나머지 원소들을 모두 비교한 이후, 꺼낸 원소의 중요도가 가장 높을 경우 카운트 1 증가
            if(isMax == true) {
                answer++;
                // 꺼낸 원소가 찾던 location일 경우
                if(first[0] == location) {
                    break;
                }
            }
        }

        return answer;
    }

    public static void main(String[] args) {
        Solution sol = new Solution();
        
        int[] priorities = new int[]{2, 1, 3, 2};
        int location = 2;

        sol.solution(priorities, location);
    }
}

회고



출처


  • 해당 문제의 저작권은 문제를 만든이에게 있으며 자세한 내용은 문제 링크에서 참조바랍니다.