https://school.programmers.co.kr/learn/courses/30/lessons/42587
큐
우선순위 큐
2가지 이용해서 현재 우선순위 가장 높은 것 빠르게 찾기
import java.util.*;
import java.io.*;
class Solution {
public int solution(int[] priorities, int location) {
int answer = 0;
PriorityQueue<Item> pq = new PriorityQueue<>();
Deque<Item> queue = new ArrayDeque<>();
for(int i = 0; i < priorities.length; i++) {
Item o = new Item(i, priorities[i]);
queue.add(o);
pq.add(o);
}
while(true){
Item current = queue.pollFirst();
if(current.value < pq.peek().value) { // 우선순위가장 높은거보다 작다면
queue.addLast(current); // 다시 큐에 넣음
} else {
answer += 1; // 프린트할 거니까
if(current.i == location)break;
pq.poll(); // 다음 우선순위 구하기 위해 진행
}
}
return answer;
}
static class Item implements Comparable<Item>{
int i; // location
int value;
Item(int i , int value) {
this.i = i;
this.value = value;
}
@Override
public int compareTo(Item o){
return o.value - this.value ;
}
}
}
[프로그래머스] 방문길이 (0) | 2025.02.03 |
---|---|
[프로그래머스] 롤케이크 (0) | 2025.02.03 |
[프로그래머스] k진수에서 소수 개수 구하기 (0) | 2025.02.01 |
[프로그래머스] [1차] 뉴스 클러스터링 (0) | 2025.02.01 |
[프로그래머스] 전화번호 목록 (0) | 2025.01.31 |