Programmers_이중우선순위큐_java
2023. 2. 23. 12:05ㆍAlgorithm/Programmers
728x90
https://school.programmers.co.kr/learn/courses/30/lessons/42628
import java.util.*;
class Solution {
public int[] solution(String[] operations) {
int[] answer = {0, 0};
List<Integer> list = new LinkedList<>();
int operationsSize = operations.length;
for(int i = 0; i< operationsSize; i++){
String[] c = operations[i].split(" ");
command(c[0], Integer.parseInt(c[1]), list);
}
if(list.size() != 0){
Collections.sort(list);
answer[0] = list.get(list.size()-1);
answer[1] = list.get(0);
}
return answer;
}
public void command(String c, int num, List<Integer> list){
switch(c){
case "I":
list.add(num);
break;
case "D":
int size = list.size();
Collections.sort(list); // 오름차순 정렬
if(size != 0){
if (num == 1){
list.remove(size-1);
}else if(num == -1){
list.remove(0);
}
}
break;
}
}
}
'Algorithm > Programmers' 카테고리의 다른 글
programmers_최솟값 만들기_java (0) | 2023.02.27 |
---|---|
programmers_올바른 괄호_java (0) | 2023.02.27 |
programmers_JadenCase 문자열 만들기_java (0) | 2023.02.27 |
programmers_최댓값과 최솟값_java (0) | 2023.02.24 |
Programmers_정수 삼각형_java (0) | 2023.02.22 |