33class Solution {
44 public int solution (int [] priorities , int location ) {
55 Queue <Node > processes = new LinkedList <>();
6+ PriorityQueue <Integer > pq = new PriorityQueue <>(Collections .reverseOrder ());
7+
68
79 for (int i =0 ; i <priorities .length ; i ++){
810 processes .offer (new Node (priorities [i ], i ));
11+ pq .offer (priorities [i ]); //우선순위 값 역순 정렬 후 저장
912 }
1013
1114 int count = 0 ;
@@ -14,24 +17,35 @@ public int solution(int[] priorities, int location) {
1417
1518 Node current = processes .poll ();
1619
17- boolean hasHigher = false ;
18-
19- for (Node node : processes ){ //우선순위가 높은 노드 찾기
20- if (node .priority > current .priority ){
21- hasHigher = true ;
22- break ;
23- }
24- }
25-
26- if (hasHigher ){ //우선순위가 높은 노드를 찾았을 때, 현재 노드를 뽑지 않고 뒤에 넣음
27- processes .offer (current );
28- }else { //자기 자신이 우선순위가 가장 높다면, 현재 뽑은 노드(자기 자신)를 제거하고 count++
20+ if (current .priority == pq .peek ()){ //현재 노드의 우선순위가 가장 높은 경우(pq의 제일 앞쪽의 값과 같음)
21+ pq .poll ();
2922 count ++;
3023
31- if (current .index == location ){ //현재 노드가 찾던 위치의 노드일때
24+ if (current .index == location ){ //현재 노드의 위치가 찾으려는 위치인 경우
3225 return count ;
3326 }
27+ }else {
28+ processes .offer (current ); //현재 노드보다 우선순위가 높은게 있는 경우, 다시 큐 뒤에 삽입
3429 }
30+
31+ // boolean hasHigher = false;
32+
33+ // for(Node node : processes){ //우선순위가 높은 노드 찾기
34+ // if(node.priority > current.priority){
35+ // hasHigher = true;
36+ // break;
37+ // }
38+ // }
39+
40+ // if(hasHigher){ //우선순위가 높은 노드를 찾았을 때, 현재 노드를 뽑지 않고 뒤에 넣음
41+ // processes.offer(current);
42+ // }else{ //자기 자신이 우선순위가 가장 높다면, 현재 뽑은 노드(자기 자신)를 제거하고 count++
43+ // count++;
44+
45+ // if(current.index == location){ //현재 노드가 찾던 위치의 노드일때
46+ // return count;
47+ // }
48+ // }
3549 }
3650
3751 return count ;
0 commit comments