forked from alexprut/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
36 lines (32 loc) · 1.03 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
TreeMap<Integer, Integer> heap = new TreeMap<Integer, Integer>();
Stack<Integer> stack = new Stack<Integer>();
for (int i = 0, N = sc.nextInt(); i < N; i++) {
int type = sc.nextInt();
if (type == 1) {
int x = sc.nextInt();
stack.push(x);
if (heap.containsKey(x)) {
heap.put(x, heap.get(x) + 1);
} else {
heap.put(x, 1);
}
} else if (type == 2) {
int x = stack.pop();
if (heap.get(x) == 1) {
heap.remove(x);
} else {
heap.put(x, heap.get(x) - 1);
}
} else {
if (heap.size() > 0) {
System.out.println(heap.lastKey());
}
}
}
}
}