본문 바로가기
알고리즘 & 자료구조/프로그래머스

프린터

by 신재권 2023. 1. 19.
package programmers;

import java.util.LinkedList;
import java.util.Queue;

public class 프린터 {

   //1. 인쇄 대기목록의 가장 앞에 있는 문저 J를 대기목록에서 꺼낸다.
   //2. 나머지 인쇄 대기목록에서 J보다 중요도가 높은 문서가 한 개라도 존재하면, J를 대기목록의 가장 마지막에 넣는다.
   //3. 그렇지 않으면 J를 인쇄한다.

   public static int solution(int[] priorities, int location) {
      Queue<Pair> q = init(priorities, location);

      int answer = 0;
      int order = 0;
      while (!q.isEmpty()) {
         Pair poll = q.poll();
         boolean state = false;

         for (Pair p : q) {
            if (poll.value < p.value) {
               state = true;
               break;
            }
         }

         if (state) {
            q.offer(poll);
            continue;
         }

         order++;

         if (poll.location) {
            answer = order;
         }
      }

      return answer;
   }

   private static Queue<Pair> init(int[] priorities, int location) {
      Queue<Pair> q = new LinkedList<>();

      for (int i = 0; i < priorities.length; i++) {
         boolean state = false;
         if (i == location) {
            state = true;
         }
         q.offer(new Pair(priorities[i], state));
      }

      return q;
   }

   private static class Pair {
      int value;
      boolean location;

      public Pair(int value, boolean location) {
         this.value = value;
         this.location = location;
      }
   }

   public static void main(String[] args) {
      System.out.println(solution(new int[] {2, 1, 3, 2}, 2));
      System.out.println(solution(new int[] {1, 1, 9, 1, 1, 1}, 0));
   }
}

'알고리즘 & 자료구조 > 프로그래머스' 카테고리의 다른 글

전화번호 목록  (0) 2023.01.21
뉴스 클러스터링  (0) 2023.01.20
N^2 배열 자르기  (0) 2023.01.18
튜플  (0) 2023.01.16
괄호 회전하기  (0) 2023.01.14