프로그래머스 Stack/Queue > 프린터 [자바, JAVA]

: ) YOUNG·2021년 12월 5일
2

알고리즘

목록 보기
28/370
post-thumbnail

문제

https://programmers.co.kr/learn/courses/30/lessons/42587?language=java

일반적인 프린터는 인쇄 요청이 들어온 순서대로 인쇄합니다. 그렇기 때문에 중요한 문서가 나중에 인쇄될 수 있습니다. 이런 문제를 보완하기 위해 중요도가 높은 문서를 먼저 인쇄하는 프린터를 개발했습니다. 이 새롭게 개발한 프린터는 아래와 같은 방식으로 인쇄 작업을 수행합니다.

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

예를 들어, 4개의 문서(A, B, C, D)가 순서대로 인쇄 대기목록에 있고 중요도가 2 1 3 2 라면 C D A B 순으로 인쇄하게 됩니다.

내가 인쇄를 요청한 문서가 몇 번째로 인쇄되는지 알고 싶습니다. 위의 예에서 C는 1번째로, A는 3번째로 인쇄됩니다.

현재 대기목록에 있는 문서의 중요도가 순서대로 담긴 배열 priorities와 내가 인쇄를 요청한 문서가 현재 대기목록의 어떤 위치에 있는지를 알려주는 location이 매개변수로 주어질 때, 내가 인쇄를 요청한 문서가 몇 번째로 인쇄되는지 return 하도록 solution 함수를 작성해주세요.


제한사항

  • 현재 대기목록에는 1개 이상 100개 이하의 문서가 있습니다.
  • 인쇄 작업의 중요도는 1~9로 표현하며 숫자가 클수록 중요하다는 뜻입니다.
  • location은 0 이상 (현재 대기목록에 있는 작업 수 - 1) 이하의 값을 가지며 대기목록의 가장 앞에 있으면 0, 두 번째에 있으면 1로 표현합니다.

입출력 예

prioritieslocationreturn
[2, 1, 3, 2]21
[1, 1, 9, 1, 1, 1]05

입출력 예 설명

예제 #1
문제에 나온 예와 같습니다.

예제 #2

6개의 문서(A, B, C, D, E, F)가 인쇄 대기목록에 있고 중요도가 1 1 9 1 1 1 이므로 C D E F A B 순으로 인쇄합니다.


코드

Class를 사용해서 해결

import java.util.ArrayList;
import java.util.List;

class Solution {
    public int solution(int[] priorities, int location) {
        // 1. Queue를 만든다
        List<PrintJob> printer = new ArrayList<PrintJob>();
        for(int i=0; i<priorities.length; i++) {
            printer.add(new PrintJob(i, priorities[i]));
        }

        int turn = 0;
        while(!printer.isEmpty()) {
            // 2. 0번을 꺼내서 max priority가 아니면 다시 끝에 넣는다.
            PrintJob job = printer.remove(0);
            if(printer.stream().anyMatch(otherJob -> job.priority < otherJob.priority)) {
                printer.add(job);
            }
            else {
                // 3. max priority이면 내가 찾는 job이 맞는지 확인한다.
                turn ++;
                if(location == job.location) {
                    break;
                }
            }
        }
        return turn;
    }
  
    class PrintJob {
        int priority;
        int location;

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

Class 사용 X

import java.util.*;

class Solution {
    public int solution(int[] priorities, int location) {
        // 1. Queue 만들기
        ArrayList<Integer> printer = new ArrayList<>();
        for(int p : priorities) {
            printer.add(p);
        }

        int turn = 0;
        // 2. 0번을 꺼내서 max priority가 아니면 다시 끝에 넣는다.
        while(!printer.isEmpty()) {
            Integer priority = printer.remove(0);

            // printer에서 [anyMatch의 의미=> 다른 하나라도] priority보다 큰 값이 있다면 
            // priority를 printer에 추가해라. 
            if(printer.stream().anyMatch(otherPriority -> priority < otherPriority)) {
                printer.add(priority);
            }
            else {
                // if조건 false의 경우 즉, printer에서 priority보다 큰 값이 없을 경우
                // ( 현재 선택된 priority가 가장 큰 값임을 의미)
                // 3. max priority이면 내가 찾는 job이 맞는지 확인한다.
                turn++;
                if(location == 0) {
                    break;
                }
            }
           
            // 반복해서 확인할 때 마다 위치(location)를 하나씩 줄임
            location--;
            if(location < 0) {
                location = printer.size() - 1;
            }
        }
        return turn;
    }
}

0개의 댓글