728x90
우선순위 큐
코드
import java.util.*;
class Solution {
public int solution(int[] priorities, int location) {
int answer = 0;
PriorityQueue < Job > pq = new PriorityQueue();
for (int i = 0; i < priorities.length; i++) {
Job job = new Job();
job.num = i;
job.weight = priorities[i];
pq.offer(job);
}
while (!pq.isEmpty()) {
for (int i = 0; i < priorities.length; i++) {
if (pq.peek().weight == priorities[i]) {
pq.poll();
answer++;
if (location == i) {
return answer;
}
}
}
}
return answer;
}
}
class Job implements Comparable < Job > {
int num;
int weight;
@Override
public int compareTo(Job target) {
//오름차순 정렬
if (this.weight > target.weight) {
return -1;
} else if (this.weight < target.weight) {
return 1;
}
return 0;
}
}
728x90
'Algorithm > Programmers' 카테고리의 다른 글
Programmers - 스킬트리 (0) | 2020.05.29 |
---|---|
Programmers - 땅따먹기 (0) | 2020.05.29 |
Programmers - [1차] 프렌즈4블록 (0) | 2020.05.08 |
Programmers - 문자열 압축 (0) | 2020.05.08 |
Programmers - 체육복 (0) | 2020.04.11 |
댓글