클래스를 정의하여 구현
참고 : 개발자로 취직하기 유튜브
import java.util.*;
class Solution {
public int solution(int[] priorities, int location) {
// 1. Queue 를 만들기
List<PrintJob> printer = new ArrayList<PrintJob>();
for(int i = 0; i < priorities.length; i++){
printer.add(new PrintJob(i, priorities[i]));
}
int turn = 0;
while(!printer.isEmpty()) {
// 2. 0 번을 꺼내서 max priority 가 아니면 끝에 넣는다.
PrintJob job = printer.remove(0);
// 현재 job 보다 우선 순위가 높은 job 이 있으면
if(printer.stream().anyMatch(otherJob -> job.priority < otherJob.priority)) {
printer.add(job); // 다시 넣어줌
}
// 3. max priority 이면 내가 찾는 job 이 맞는지 확인한다.
else {
turn++;
// 구하려고 한 위치의 job 이 맞는지
if(location == job.location) {
break;
}
}
}
return turn;
}
class PrintJob {
int priority;
int location;
PrintJob(int location, int priority) {
this.location = location;
this.priority = priority;
}
}
}
'solve' 카테고리의 다른 글
| [프로그래머스/BFS] Lv2. 게임 맵 최단 거리 (자바) (0) | 2024.01.13 |
|---|---|
| [프로그래머스/큐] Lv2. 기능 개발 (자바) (0) | 2024.01.12 |
| [백준 1764/해시] 실버4/듣보잡 (자바) (0) | 2024.01.11 |
| [프로그래머스/해시] Lv1. 신고 결과 받기(자바) (0) | 2024.01.11 |
| [프로그래머스/해시] Lv2. 순위 검색 (자바) (1) | 2024.01.11 |