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

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

by 유줘니 2019. 4. 25.

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

알고리즘 참고 : https://developerdk.tistory.com/15

import java.util.LinkedList;

class Document {
	int idx;
	int priority;
	public Document(int idx, int priority) {
		super();
		this.idx = idx;
		this.priority = priority;
	}
}

public class LESSEON42587 {

	public static void main(String[] args) {
		int[] priorities = {1, 1, 9, 1, 1, 1};
		int location = 0;
		
		System.out.println(solution(priorities, location));

	}
	
	public static int solution(int[] priorities, int location) {
        int answer = 1;
        
        LinkedList<Document> list = new LinkedList();
        //Document DTO를 담을 LinkedList생성
        
        for(int i = 0; i < priorities.length;i++) {
        	list.add(new Document(i, priorities[i]));
        }
        //idx(location)를 0번부터 지정하여 LinkedList에 Document 인스턴스를 하나씩 담음
        
        while(list.size()>1) {	//?
        	Document firstDoc = list.getFirst();
        
        	for(int i = 1; i < list.size(); i++) {
        		if(firstDoc.priority<list.get(i).priority) {
        			list.addLast(firstDoc);
        			list.removeFirst();
        			break;
        		}
        		
        		if(i == list.size()-1) {
        			if(firstDoc.idx==location) return answer;
        			list.removeFirst();
        			answer++;
        		}
        	}
        }
        return answer;
    }
}

댓글