[Programmers] 스택/큐 - 프린터

zzenee·2022년 5월 3일
0

Algorithm&Coding-test

목록 보기
6/30
post-thumbnail

import java.util.*;

class Paper {
    int location;
    int priority;
    public Paper (int location, int priority) {
        this.location = location;
        this.priority = priority;
    }
}

class Solution {
    
    public int solution(int[] priorities, int location) {
        int answer = 0;
        Queue<Paper> q = new LinkedList<>();
        
        for (int i=0; i<priorities.length; i++) {
            q.offer(new Paper(i, priorities[i]));
        }
        
        while (!q.isEmpty()) {
            Paper target = q.poll();
            for (Paper x: q) {
                if (x.priority > target.priority) {
                    q.offer(target);
                    target = null;
                    break;
                }
            }
            if (target != null) {
                answer++;
                if (target.location == location) {
                    return answer;
                }
            }
        }
        return answer;
    }
}
profile
꾸준히

0개의 댓글