알고리즘/프로그래머스
[프로그래머스] 프린터 - 자바
binghe819
2020. 7. 4. 02:30
https://programmers.co.kr/learn/courses/30/lessons/42587
코딩테스트 연습 - 프린터
일반적인 프린터는 인쇄 요청이 들어온 순서대로 인쇄합니다. 그렇기 때문에 중요한 문서가 나중에 인쇄될 수 있습니다. 이런 문제를 보완하기 위해 중요도가 높은 문서를 먼저 인쇄하는 프린��
programmers.co.kr
1. 풀이
다 풀고나니 여러가지의 풀이 방법이 있는 것 같다.
천재들은 역시 많군..
내가 생각한 풀이 방법은 큐에 우선순의(priorities)의 인덱스를 넣는 것이다.
풀다보니 큐를 두개나 생성하여 사용했는데, 다른 풀이를 더 참고하여 수정해야겠다.
2. 코드
import java.util.*;
class Solution {
public int solution(int[] priorities, int location) {
int answer = 0;
Queue<Integer> queue = new LinkedList<>();
Queue<Integer> tmpQueue = new LinkedList<>();
ArrayList<Integer> printed = new ArrayList<>();
// 인덱스 queue에 넣기
for(int i = 0; i < priorities.length; i++)
queue.offer(i);
// 인쇄하기
int checkNum;
int tmp;
boolean checkBigger;
while(!queue.isEmpty()){
checkNum = queue.poll(); // 확인할 우선순위의 인덱스
checkBigger = false;
while(!queue.isEmpty()){
tmp = queue.poll();
// 더 큰 우선순위가 있는지 확인
if(priorities[checkNum] < priorities[tmp]){
checkBigger = true;
}
tmpQueue.offer(tmp);
}
// 다시 되돌리기
while(!tmpQueue.isEmpty()){
queue.offer(tmpQueue.poll());
}
if(checkBigger){ // 만약 더 큰 우선순위가 있다면
queue.offer(checkNum);
} else {
printed.add(checkNum);
}
}
// 인쇄된 리스트 중 Location 찾기
for(int i = 0; i < printed.size(); i++){
if(printed.get(i) == location)
answer = i+1;
}
return answer;
}
}