プログラマ[スタック/キュー]プリンタ

1642 ワード

注意:https://velog.io/@qweadzs/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4-%ED%94%84%EB%A6%B0%ED%84%B0-java
import java.util.*;

public class Solution {

    public static int solution(int[] priorities, int location) {
        int answer = 0;

        Queue<Printer> q = new LinkedList<>();

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

        while (!q.isEmpty()) {
            boolean flag = false;
            Printer now = q.poll();
            for(Printer p : q) {
                if(now.priority < p.priority) {
                    flag = true;
                    break;
                }
            }
            if(flag) {
                q.offer(now);
            } else {
                    if(now.location == location)
                        answer = priorities.length - q.size();
                }
            }

        return answer;
    }
/* test용
    public static void main(String[] args) {
        int[] priorities = {2, 1, 3, 2};
        int location = 2;
        System.out.println(solution(priorities, location));
    }
*/

}

class Printer {
    int location;
    int priority;
    Printer (int l, int p) {
        this.location = l;
        this.priority = p;
    }
}