programmers.co.kr/learn/courses/30/lessons/42587
코딩테스트 연습 - 프린터
일반적인 프린터는 인쇄 요청이 들어온 순서대로 인쇄합니다. 그렇기 때문에 중요한 문서가 나중에 인쇄될 수 있습니다. 이런 문제를 보완하기 위해 중요도가 높은 문서를 먼저 인쇄하는 프린
programmers.co.kr
public class 프린터 {
public static void main(String[] args) {
프린터 service = new 프린터();
System.out.println(service.solution(new int[] {2,1,3,2}, 2));
}
public int solution(int[] priorities, int location) {
int answer = 0;
PriorityQueue<Integer> pq = new PriorityQueue<Integer>(Collections.reverseOrder());
Queue<Integer> q = new LinkedList<Integer>();
for(int i=0; i< priorities.length; i++) {
pq.offer(priorities[i]);
q.offer(priorities[i]);
}
while(true) {
int temp = q.poll();
if(pq.peek() == temp) {
answer++;
if(location == 0) {
break;
}
pq.poll();
}
else {
q.offer(temp);
}
location--;
if(location == -1) {
location = q.size()-1;
}
}
return answer;
}
}
소요시간 : 22분
프린터가 FIFO의 형태로 문서를 처리하는데 처리할 때 출력할지 안할지를 결정하는 것은 인쇄 작업의 중요도에 따라 결정되기 때문에 큐와 우선순위큐 두개를 이용해서 문제를 해결하였다.
location을 통해 해당 위치의 문서가 언제 출력될 지 계산하기 위해서 문서가 처리할 때마다 location 위치를 동기화 하였다.
'알고리즘' 카테고리의 다른 글
[프로그래머스] 이중우선순위큐 - Heap(lv3) (0) | 2020.12.29 |
---|---|
[프로그래머스] 디스크컨트롤러 - Heap(lv3) (0) | 2020.12.29 |
[프로그래머스] 전화번호 목록 - 해시 (0) | 2020.12.18 |
[프로그래머스] 위장 - 해시 (0) | 2020.12.18 |
[프로그래머스] 탐욕법 - 구명보트 (2) | 2020.12.14 |