230217 프린터

Jongleee·2023년 2월 17일
0

TIL

목록 보기
184/576

public class Printer {

public static int solution(int[] priorities, int location) {
    Deque<Document> queue = new LinkedList<>();
    int answer = 0;

    for (int i = 0; i < priorities.length; i++) {
        queue.add(new Document(i, priorities[i]));
    }

    while (!queue.isEmpty()) {
        Document currentDoc = queue.poll();

        boolean hasHigherPriority = queue.stream().anyMatch(doc -> doc.priority > currentDoc.priority);

        if (hasHigherPriority) {
            queue.addLast(currentDoc);
        } else {
            answer++;
            if (currentDoc.index == location) {
                return answer;
            }
        }
    }

    return answer;
}

private static class Document {
    int index;
    int priority;

    public Document(int index, int priority) {
        this.index = index;
        this.priority = priority;
    }
}

0개의 댓글