원본 문제 : 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;
}
}
'프로그래머스 > 코딩연습1' 카테고리의 다른 글
[프로그래머스] 탑(42588) (java) (0) | 2019.04.30 |
---|---|
[프로그래머스] 체육복(42862)(java) (0) | 2019.04.30 |
[프로그래머스] 기능개발(42586)(java) (0) | 2019.04.29 |
[프로그래머스] 실패율 (42889)(JAVA) (0) | 2019.04.25 |
[프로그래머스] 다리를 지나는 트럭(42583) (java) (0) | 2019.04.25 |
댓글