-
[programmers] 이중 우선순위 큐Algorithm/java tip 2021. 3. 6. 14:35
programmers.co.kr/learn/courses/30/lessons/42628
import java.util.*; class Solution { public int[] solution(String[] operations) { PriorityQueue<Integer> minHeap = new PriorityQueue<>(); PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder()); for(String op : operations) { int val = Integer.parseInt(op.substring(1).trim()); if(op.charAt(0) == 'I') { // 숫자 삽입 minHeap.offer(val); maxHeap.offer(val); } else if(op.charAt(0) == 'D') { // 숫자 삭제 if(val == 1) { // 최대값 삭제 if(!maxHeap.isEmpty()) { minHeap.remove(maxHeap.poll()); } } else if(val == -1) { // 최소값 삭제 if(!minHeap.isEmpty()){ maxHeap.remove(minHeap.poll()); } } } } int[] ans = new int[2]; if(!maxHeap.isEmpty() || !minHeap.isEmpty()) { ans[0] = maxHeap.peek(); ans[1] = minHeap.peek(); } return ans; } }
'Algorithm > java tip' 카테고리의 다른 글
[programmers] 큰 수 만들기 (0) 2021.03.06 [programmers] 체육복 (0) 2021.03.06 [programmers] 더 맵게(heap) (0) 2021.03.06 [programmers] 소수 찾기 (0) 2021.03.05 107. Binary Tree Level Order Traversal II (0) 2021.03.03