[Programmers] 프린터 - JAVA

ONE·2022년 2월 17일
0

Programmers

목록 보기
9/24

📚 Problem

프린터

  • 중요도가 높은 문서를 먼저 인쇄하는 프린트
  1. 인쇄 대기목록의 가장 앞에 있는 문서(J)를 대기목록에서 꺼냅니다.
  2. 나머지 인쇄 대기목록에서 J보다 중요도가 높은 문서가 한 개라도 존재하면 J를 대기목록의 가장 마지막에 넣습니다.
  3. 그렇지 않으면 J를 인쇄합니다.
  • 내가 인쇄를 요청한 문서가 현재 대기목록의 어떤 위치에 있는지를 알려주는 location 이 매개변수로 주어질 때, 내가 인쇄를 요청한 문서가 몇 번째로 인쇄되는지 찾기

📝 Solution

Key Idea

  • 중요도처음 위치의 인덱스 값을 가진 class Paper 생성
  • Queue 를 만들어 모두 삽입한 후
  • 하나씩 꺼내어 큐에 남은 Paper 들 중 자신의 우선순위 보다 높은 Paper 가 있는지 찾기
  • 만약 자신의 우선순위보다 큰 Paper 가 있다면 꺼내었던 Paper 를 다시 Queue 에 삽입
  • 위를 반복하다가 자신의 문서(location)인 Paper 를 찾는다면 이때 까지 꺼낸수를 답에 넣는다
public int solution(int[] priorities, int location) {
    int answer = 0;
    Queue<Paper> queue = initQueue(priorities);

    while (!queue.isEmpty()) {
        Paper first = queue.poll();
        if (!isMostImportant(queue, first)){
            queue.add(first);
            continue;
        }
        answer++;
        if(first.isMyPaper(location))
            break;
    }

    return answer;
}

💻 Code

Solution.java

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

class Paper {
    int priority;
    int index;

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

    public boolean isMyPaper(int location) {
        return this.index == location;
    }
}

class Solution {
    public int solution(int[] priorities, int location) {
        int answer = 0;
        Queue<Paper> queue = initQueue(priorities);

        while (!queue.isEmpty()) {
            Paper first = queue.poll();
            if (!isMostImportant(queue, first)){
                queue.add(first);
                continue;
            }
            answer++;
            if(first.isMyPaper(location))
                break;
        }

        return answer;
    }

    public Queue<Paper> initQueue(int[] priorities) {
        Queue<Paper> queue = new LinkedList<>();

        for (int i = 0; i < priorities.length; i++)
            queue.add(new Paper(priorities[i],i));

        return queue;
    }

    private boolean isMostImportant(Queue<Paper> queue, Paper current) {
        for (Paper paper : queue)
            if(current.priority < paper.priority)
                return false;
        return true;
    }
}

SolutionTest.java

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class SolutionTest {
    Solution solution;

    @BeforeEach
    public void setSol(){
        solution = new Solution();
    }

    @Test
    public void solution_1(){
        int result = solution.solution(new int[]{2, 1, 3, 2}, 2);
        assertEquals(1, result);
    }

    @Test
    public void solution_2(){
        int result = solution.solution(new int[]{1, 1, 9, 1, 1, 1}, 0);
        assertEquals(5, result);
    }
}
profile
New, Strange, Want to try

0개의 댓글