Skip to content

Commit 03eb7f4

Browse files
committed
[Heap] programmers-이중우선순위큐
1 parent c165e11 commit 03eb7f4

File tree

5 files changed

+52
-2
lines changed

5 files changed

+52
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@
112112
| :-: | :-: | :-------------------------------------- | :------------------------------- |
113113
| 01 | | [Baekjoon-10845 큐](./src/Queue/P10845) | |
114114
| 02 | | [Baekjoon-3190 뱀](./src/Queue/P3190) | Deque / 삼성 SW 역량 테스트 기출 |
115+
| 03 | | [Programmers 디스크 컨트롤러](src/Queue/prg42627) | Priority Queue |
116+
| 04 | | [Programmers 이중우선순위큐](src/Queue/prg42628) | Priority Queue |
115117

116118
### Tree
117119

@@ -127,7 +129,6 @@
127129
| 02 | | [Baekjoon-1927 최소 힙](./src/Heap/P1927) | |
128130
| 03 | | [Baekjoon-11279 최대 힙](./src/Heap/P11279) | |
129131
| 04 | | [Programmers 더 맵게](./src/Heap/prg42626) | |
130-
| 05 | | [Programmers 디스크 컨트롤러](./src/Heap/prg42627) | |
131132

132133
### Indexed Tree (Segment Tree)
133134

src/Heap/prg42627/Main.java renamed to src/Queue/prg42627/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package Heap.prg42627;
1+
package Queue.prg42627;
22

33
import java.util.Arrays;
44
import java.util.PriorityQueue;
File renamed without changes.

src/Queue/prg42628/Main.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package Queue.prg42628;
2+
3+
import java.util.*;
4+
5+
public class Main {
6+
public static void main(String[] args) {
7+
Solution sol = new Solution();
8+
System.out.println(Arrays.toString(sol.solution(new String[]{"I 16", "D 1"})));
9+
// [0, 0]
10+
System.out.println(Arrays.toString(sol.solution(new String[]{"I 16", "I -5643", "D -1", "D 1", "D 1", "I 123", "D -1"})));
11+
// [0, 0]
12+
System.out.println(Arrays.toString(sol.solution(new String[]{"I 7", "I 5", "I -5", "D -1"})));
13+
// [7, 5]
14+
System.out.println(Arrays.toString(sol.solution(new String[]{"I -45", "I 653", "D 1", "I -642", "I 45", "I 97", "D 1", "D -1", "I 333"})));
15+
// [333, -45]
16+
}
17+
}
18+
19+
class Solution {
20+
public int[] solution(String[] operations) {
21+
PriorityQueue<Integer> pqMin = new PriorityQueue<>(((o1, o2) -> o1 - o2));
22+
PriorityQueue<Integer> pqMax = new PriorityQueue<>(((o1, o2) -> o2 - o1));
23+
24+
for (String op : operations) {
25+
StringTokenizer st = new StringTokenizer(op);
26+
char cmd = st.nextToken().charAt(0);
27+
int num = Integer.parseInt(st.nextToken());
28+
29+
if (cmd == 'I') {
30+
pqMin.add(num);
31+
pqMax.add(num);
32+
} else if (cmd == 'D' && !pqMin.isEmpty()) {
33+
if (num == 1) {
34+
int target = pqMax.poll();
35+
pqMin.remove(target);
36+
} else if (num == -1) {
37+
int target = pqMin.poll();
38+
pqMax.remove(target);
39+
}
40+
}
41+
}
42+
43+
if (pqMin.isEmpty()) return new int[]{0, 0};
44+
else return new int[]{pqMax.peek(), pqMin.peek()};
45+
}
46+
}

src/Queue/prg42628/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## [programmers - 힙(Heap)] 이중우선순위큐
2+
3+
![image](https://user-images.githubusercontent.com/22045163/106357160-b4ad0d80-6347-11eb-943e-46361de1664f.png)

0 commit comments

Comments
 (0)