# ★★☆☆☆ 프린터

# 구현코드

import java.util.Deque;
import java.util.LinkedList;

public class Solution {
    public int solution(int[] priorities, int location) {
        Deque<Word> printer = new LinkedList<>();
        int maxPrior = 0;
        for (int i = 0; i < priorities.length; i++) {
            maxPrior = maxPrior < priorities[i] ? priorities[i] : maxPrior;
            printer.add(new Word(priorities[i], location == i));
        }

        int count = 0;
        boolean isOk = false;
        while(!isOk){
            Word word = printer.removeFirst();
            if(word.prior == maxPrior){
                count++;
                if(word.isMine)
                    isOk = true;
                else
                    maxPrior = findMaxPrior(printer);
            }
            else{
                printer.addLast(word);
            }
        }

        return count;
    }

    public int findMaxPrior(Deque<Word> deque) {
        int max = 0;
        for (int i = 0; i < deque.size(); i++) {
            Word word = deque.removeFirst();
            max = word.prior > max ? word.prior : max;
            deque.addLast(word);
        }
        return max;
    }

    public class Word {
        int prior;
        boolean isMine;

        public Word(int p, boolean isMine) {
            prior = p;
            this.isMine = isMine;
        }
    }
}
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
51