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;
}
}