[프로그래머스 / Level2] 프린터 (Java)

문제 보기



풀이

  • 대기목록을 구현하기 위하여 FIFO인 Queue 사용
  • location과 priority를 같이 저장하기 위하여 Map 사용
  • 가장 우선순위가 높은지 비교하기 위해 isHighestPriority() 메소드 구현
class Solution {
    public int solution(int[] priorities, int location) {
        Map<Integer, Integer> map = new HashMap<>();
        Queue<Integer> queue = new LinkedList<>();

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

        int answer = 1;
        while(true) {
            int front = queue.poll();
            int frontPriority = map.get(front);

            if(isHighestPrioriy(map, frontPriority)) {
                if(front == location) break;
                map.remove(front);
                answer++;
            } else {
                queue.add(front);
            }
        }

        return answer;
    }

    public boolean isHighestPrioriy(Map<Integer, Integer> map, int priority) {
        boolean ret = true;
        if (map.values().stream()
                .filter(v -> v > priority)
                .count() > 0) {
            ret = false;
        }

        return ret;
    }
}

좋은 웹페이지 즐겨찾기