본문 바로가기
프로그래머스/코딩연습2

[프로그래머스] 프린터

by 유줘니 2019. 5. 29.

원본 문제 : https://programmers.co.kr/learn/courses/30/lessons/42587

풀이 참고 : https://developerdk.tistory.com/15

 

 

import java.util.LinkedList;
import java.util.List;

class Document {
	int priority;
	int index;
	
	Document(int priority, int index) {
		this.priority = priority;
		this.index = index;	
	}
}

class Solution {
	public static int solution(int[] priorities, int location) {
		
		int answer = 1;
		
		LinkedList<Document> list = new LinkedList<Document>();
		for(int i = 0 ; i < priorities.length ; i++)
			list.add(new Document(priorities[i], i));
			
		while(list.size()>1) {
			Document firstDoc = list.getFirst();
			
			for(int i = 1; i < list.size(); i++) {
				if(firstDoc.priority < list.get(i).priority) {
					list.removeFirst();
					list.add(firstDoc);
					break;
				}
				
				if(i == list.size()-1) {
					if(firstDoc.index==location) return answer;
					list.removeFirst();
					answer++;
				}
			}
		}
		return answer;
	}
}

댓글