11import java .util .*;
22class Solution {
33 public int [] solution (String [] operations ) {
4- // 오름차순 우선순위 큐
5- PriorityQueue <Integer > ascPq = new PriorityQueue <>();
6- // 내림차순 우선순위 큐
7- PriorityQueue <Integer > descPq = new PriorityQueue <>((a ,b ) -> b -a );
4+ PriorityQueue <Integer > apq = new PriorityQueue <>(); // 오름차순
5+ PriorityQueue <Integer > dpq = new PriorityQueue <>((a ,b ) -> b - a ); // 내림차순
86
97 int size = 0 ;
10- for (String operation : operations ){
11- String [] params = operation .split (" " );
12- String command = params [0 ]; // 명령어
13- String number = params [1 ]; // 숫자
14- if (command .equals ("I" )){ // 삽입 명령어
15- ascPq .offer (Integer .parseInt (number ));
16- descPq .offer (Integer .parseInt (number ));
8+
9+ for (String operation : operations ){
10+ String command = operation .split (" " )[0 ];
11+ String numStr = operation .split (" " )[1 ];
12+
13+ if (command .equals ("I" )){
14+ int num = Integer .parseInt (numStr );
15+ apq .offer (num );
16+ dpq .offer (num );
1717 size ++;
18- } else { // 삭제 명령어
18+ } else {
1919 if (size > 0 ){
20- if (number .equals ("-1" )) { // 최솟값 삭제
21- descPq .remove (ascPq .poll ());
20+ if (numStr .equals ("-1" )) { // 최솟값 삭제
2221 size --;
23- } else { // 최댓값 삭제
24- ascPq . remove ( descPq . poll ());
22+ dpq . remove ( apq . poll ());
23+ } else { // 최댓값 삭제
2524 size --;
25+ apq .remove (dpq .poll ());
2626 }
2727 }
2828 }
2929 }
30- if (size == 0 ) return new int []{0 ,0 };
31- return new int []{ descPq .poll (),ascPq .poll ()};
30+ if (size == 0 ) return new int [] {0 ,0 };
31+ return new int [] { dpq .poll (), apq .poll ()};
3232 }
3333}
0 commit comments