Open
Description
package algo;
import java.util.Collections;
import java.util.PriorityQueue;
public class Solution {
public int[] solution(String[] operations) {
int[] answer = new int[2];
PriorityQueue<Integer> maxQ = new PriorityQueue<>(Collections.reverseOrder()); // 내림차순 맨 앞 max
PriorityQueue<Integer> minQ = new PriorityQueue<>(); //오름차순 맨 앞 min
/** {"I 4", "I 3", "I 2", "I 1", "D 1", "D 1", "D -1", "D -1", "I 5", "I 6"}; 같은 경우를 생각해야한다*/
for (int i = 0; i < operations.length; i++)
{
if (!maxQ.isEmpty() && !minQ.isEmpty() && maxQ.peek() < minQ.peek())
{
maxQ = new PriorityQueue<>(Collections.reverseOrder());
minQ = new PriorityQueue<>();
}
if (operations[i].charAt(0) == 'I')
{
if (operations[i].charAt(2) == '-')
{
// 16같은경우 1만 들어감
String substring = operations[i].substring(3, operations[i].length());
minQ.add(-Integer.parseInt(substring));
maxQ.add(-Integer.parseInt(substring));
} else {
String substring = operations[i].substring(2, operations[i].length());
minQ.add(Integer.parseInt(substring));
maxQ.add(Integer.parseInt(substring));
}
}
if (operations[i].charAt(0) == 'D')
{
if (operations[i].charAt(2) == '-' && !minQ.isEmpty())
{
minQ.remove();
}
else if (operations[i].charAt(2) != '-' && !maxQ.isEmpty())
{
maxQ.remove();
}
}
}
// 최대값이 비었는데 최소값이 존재할 수 없다.
if (maxQ.isEmpty())
{
answer[0] = 0;
answer[1] = 0;
}else{
answer[0] = maxQ.peek();
answer[1] = minQ.peek();
}
return answer;
}
}