-
Notifications
You must be signed in to change notification settings - Fork 11
/
MaximumElement.java
31 lines (27 loc) Β· 950 Bytes
/
MaximumElement.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
// https://www.hackerrank.com/challenges/maximum-element/problem
package stacks;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.Stack;
public class MaximumElement {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int queries = scanner.nextInt();
PriorityQueue<Integer> queue = new PriorityQueue<>((o1, o2) -> Integer.compare(o2, o1));
Stack<Integer> stack = new Stack<>();
while (queries-- > 0) {
int type = scanner.nextInt();
if (type == 1) {
int element = scanner.nextInt();
stack.add(element);
queue.add(element);
} else if (type == 2) {
int element = stack.pop();
queue.remove(element);
} else {
System.out.println(queue.peek());
}
}
}
}