알고리즘/프로그래머스
프린터
가다파하
2020. 10. 19. 00:33
출처 - https://programmers.co.kr/learn/courses/30/lessons/42587
코딩테스트 연습 - 프린터
일반적인 프린터는 인쇄 요청이 들어온 순서대로 인쇄합니다. 그렇기 때문에 중요한 문서가 나중에 인쇄될 수 있습니다. 이런 문제를 보완하기 위해 중요도가 높은 문서를 먼저 인쇄하는 프린��
programmers.co.kr
import java.util.LinkedList;
public class Printer {
public int solution(int[] priorities, int location) {
int answer = 0;
LinkedList<Integer> queue = new LinkedList<Integer>();
LinkedList<Boolean> flags = new LinkedList<Boolean>();
for(int i=0; i<priorities.length; i++) {
queue.add(priorities[i]);
if(i==location) {
flags.add(true);
}else {
flags.add(false);
}
}
while(queue.size() > 0) {
int compareValue = queue.peek();
boolean flag = flags.peek();
boolean isExist = false;
for(int i=1; i< queue.size(); i++) {
if(queue.get(i) > compareValue) {
isExist = true;
break;
}
}
if(isExist) {
queue.pop();
queue.add(compareValue);
flags.pop();
flags.add(flag);
}else {
answer++;
if(flag) {
return answer;
}else {
queue.pop();
flags.pop();
}
}
}
return answer;
}
public static void main(String[] args) {
/*
priorities location return
[2, 1, 3, 2] 2 1
[1, 1, 9, 1, 1, 1] 0 5
priority
1~9
0 <= location
*/
int[] priorities = {2,1,3,2};
int location = 2;
System.out.println(new Printer().solution(priorities, location));
}
}