240223 이중우선순위큐

Jongleee·2024년 2월 23일
0

TIL

목록 보기
503/737
public int[] solution(String[] operations) {
	int[] answer = { 0, 0 };
	PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Comparator.reverseOrder());
	PriorityQueue<Integer> minHeap = new PriorityQueue<>();

	for (String op : operations) {
		String[] splitOp = op.split(" ");
		String command = splitOp[0];
		int value = Integer.parseInt(splitOp[1]);

		if (command.equals("I")) {
			maxHeap.add(value);
			minHeap.add(value);
		} else if (command.equals("D") && !maxHeap.isEmpty()) {
			if (value == 1) {
				int max = maxHeap.poll();
				minHeap.remove(max);
			} else {
				int min = minHeap.poll();
				maxHeap.remove(min);
			}
		}
	}

	if (!maxHeap.isEmpty()) {
		answer[0] = maxHeap.peek();
		answer[1] = minHeap.peek();
	}

	return answer;
}

출처:https://school.programmers.co.kr/learn/courses/30/lessons/42628

0개의 댓글