안드로이드 세계

[프로그래머스] 프린터 (JAVA) 본문

알고리즘/자바(JAVA)

[프로그래머스] 프린터 (JAVA)

리안94 2021. 1. 31. 01:50

출처 : programmers.co.kr/learn/courses/30/lessons/42587

 

코딩테스트 연습 - 프린터

일반적인 프린터는 인쇄 요청이 들어온 순서대로 인쇄합니다. 그렇기 때문에 중요한 문서가 나중에 인쇄될 수 있습니다. 이런 문제를 보완하기 위해 중요도가 높은 문서를 먼저 인쇄하는 프린

programmers.co.kr

 

문제 설명

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

  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로 표현합니다.

 

입출력 예

priorities location return
[2, 1, 3, 2] 2 1
[1, 1, 9, 1, 1, 1] 0 5

 

입출력 예 설명

예제 #1

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

예제 #2

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

 

이문제는 우선순위 큐(PriorityQueue)와 Deque를 이용하면 간단하게 풀리지만, 사용하지 않고 푸는 것을 기반으로 한다.

 

일단 프린터의 위치와 중요도를 가지는 모델 클래스를 하나 생성해준다.

 

class Printer {

	private int position;
	private int priority;

	public Printer(int position, int priority) {
		this.position = position;
		this.priority = priority;
	}

	public int getPosition() {
		return position;
	}

	public int getPriority() {
		return priority;
	}

}

 

그리고 Queue를 하나 생성하여 주어진 값들을 넣어준다.

Queue<Printer> print = new LinkedList<>();

for (int index = 0; index < priorities.length; index++) {
	print.add(new Printer(index, priorities[index]));
}

 

맨 앞의 값을 꺼낸후에 뒤의 값과 비교해주고, 뒤의 값 중 중요도가 높다면 꺼낸값을 다시 넣어주어야 한다.

 

while(!print.isEmpty()) {
	Boolean isAdd = false;
	Printer comparatorValue = print.poll();
			
	Iterator<Printer> value = print.iterator();
			
	while (value.hasNext()) {
		if (value.next().getPriority() > comparatorValue.getPriority()) {
			print.add(comparatorValue);
			isAdd = true;
			break;
		}
	}
			
	if (!isAdd) {
		answer++;
		if(comparatorValue.getPosition() == location)
			break;
	}
	
}

 

다시 넣어줬는지의 대한 여부인 isAdd를 false로 선언해주고, 꺼낸값을 들고 있을 변수를 선언해준 뒤 Queue에서 꺼내 준다.

 

그 후, 남아있는 Queue의 중요도와 꺼내 둔 Queue의 중요도를 비교하여 남아있는 Queue의 중요도가 더 높다면 다시 넣어주고, isAdd를 true로 바꿔준다.

 

추가하지 않았다면 isAdd는 false이며, 꺼낸횟수를 더해준다. 그리고 꺼낸값의 위치와 요청한 문서의 위치가 동일하다면 반복문을 break 한다.

 

전체코드는 다음과 같다.

public int solution(int[] priorities, int location) {
	int answer = 0;

	Queue<Printer> print = new LinkedList<>();

	for (int index = 0; index < priorities.length; index++) {
		print.add(new Printer(index, priorities[index]));
	}

	while(!print.isEmpty()) {
		Boolean isAdd = false;
		Printer comparatorValue = print.poll();
			
		Iterator<Printer> value = print.iterator();
			
		while (value.hasNext()) {
			if (value.next().getPriority() > comparatorValue.getPriority()) {
				print.add(comparatorValue);
				isAdd = true;
				break;
			}
		}
			
		if (!isAdd) {
			answer++;
			if(comparatorValue.getPosition() == location)
				break;
		}
		
	}
		
	return answer;
}

class Printer {

	private int position;
	private int priority;

	public Printer(int position, int priority) {
		this.position = position;
		this.priority = priority;
	}

	public int getPosition() {
		return position;
	}

	public int getPriority() {
		return priority;
	}

}
Comments