import java.util.*;
class Solution {
public int solution(int[] priorities, int location) {
int answer = 0;
// ํ(printer)์๋ [์ค์๋, ์ด๊ธฐ์์น] ๊ฐ ๋ด๊ธด ๋ฐฐ์ด์ ๋ฃ์ด์ค ๊ฒ
// ์ฐ์ ์์ ํ(rank)์๋ ํ๋ฆฐํฐ์ ์ค์๋๋ฅผ ๋ด๋ฆผ์ฐจ์์ผ๋ก ๋ฃ์ด์ค ๊ฒ
Queue<int[]> printer = new LinkedList<>();
PriorityQueue<Integer> rank = new PriorityQueue<>(Collections.reverseOrder());
// ํ๋ฆฐํฐ ์ ๋ณด๋ฅผ ํ์ ์ฐ์ ์์ ํ์ ๋ฃ์ด์ค๋ค.
for(int i=0;i<priorities.length;i++){
rank.offer(priorities[i]);
printer.offer(new int[] {priorities[i],i});
}
// count๋ location์ ํด๋นํ๋ ๋ฌธ์๊น์ง ์ผ๋ง๋ ๊ฑธ๋ฆด์ง ๊ธฐ๋ก
int count =1;
while(true){
//ํ๋ฆฐํฐ ์์๋๋ก ๋ฌธ์ ์ค์๋๋ฅผ ํ์
ํ๋ค.
int[] element = printer.poll();
// ์ฐ์ ์์ ํ๋ ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌ๋์ด์๊ณ , ์ฐ์ ์์ ํ peek() ํ์๋
// ์ธ์๋ฌผ์ ์ฐ์ ์์์ ๊ฐ์์ผ ์ธ์๊ฐ ๊ฐ๋ฅํ๋ค.
if(element[0]==rank.peek()){
// ๋ง์ฝ ํด๋น ๋ฌธ์์ ์์๊ฐ, ์ฐ๋ฆฌ๊ฐ ์๊ธฐ๋ฅผ ์ํ๋ ์์น์ ๋ฌธ์๋ผ๋ฉด? answer์ ๊ธฐ๋กํ๋ค.
if(element[1]==location){
answer = count;
break;
}
// ์ธ์๋์๋ค๋ฉด, ์ฐ์ ์์ ํ์์๋ poll() ์ฒ๋ฆฌํ๊ณ , count ๋ฅผ 1 ์ฆ๊ฐ์ํจ๋ค.
rank.poll();
count++;
//์ฐ์ ์์ ํ์ ๋จ์์๋ ์์๋ณด๋ค ๋ฎ์ผ๋ฉด, ๋ค์ ํ์ offerํ๋ค.
}else{
printer.offer(element);
}
}
return answer;
}
}
์ ๋ต ๋ณผ๊น ๋ง๊น ์์ฒญ ๊ณ ๋ฏผํ๋ค๊ฐ, ๋ง์ง๋ง์ผ๋ก ๋์ ํด๋ณธ ๋ฐฉ๋ฒ์ด ํ๋ ค์ ๋๋ ๋ฌธ์ !
์ผ๋จ ๋ชจ๋ ๋ฌธ์์ ์ค์๋๋ฅผ ์ด๋ป๊ฒ ํ์ ํ๊ณ ์ ๊ฑฐํด๋๊ฐ์ง ์์ฒญ ๊ณ ๋ฏผํ์๋ค.
์ฐ์ ์์ํ๋ฅผ Collections.reverseOrder()๋ก ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌํ๋๋ก ์ค์ ํด์ ์ฌ์ฉํ๋๋ฐ,
์ฐ์ ์์ ํ ๋ด๋ฆผ์ฐจ์ ์ ์ฉ์ ์ด๋ป๊ฒ ํ๋์ง ๋ชฐ๋ผ์ ์ธํฐ๋ท์ ์ณ๋ดค๋ค.
์๊น๋จน๋๋ก ๊ธฐ์ตํ๊ณ ์์ด์ผ๊ฒ ๋ค.