forked from oleg-cherednik/DailyCodingProblem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
56 lines (43 loc) · 1.31 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Queue;
/**
* @author Oleg Cherednik
* @since 16.02.2019
*/
public class Solution {
public static void main(String... args) {
int[] arr = { 9, 12, 3, 5, 14, 10, 10 };
System.out.println(Arrays.toString(arr));
Stack<Integer> stack = new Stack<>();
for (int item : arr)
stack.push(item);
for (int i = 0; i < arr.length; i++)
System.out.println(stack.pop());
}
public static final class Stack<T> {
private final Queue<Node<T>> maxHeap = new PriorityQueue<>();
private int size;
public void push(T item) {
maxHeap.add(new Node<T>(size++, item));
}
public T pop() {
if (size == 0)
throw new RuntimeException();
size--;
return maxHeap.remove().item;
}
public static final class Node<T> implements Comparable<Node<T>> {
private final int pos;
private final T item;
public Node(int pos, T item) {
this.pos = pos;
this.item = item;
}
@Override
public int compareTo(Node<T> obj) {
return Integer.compare(obj.pos, pos);
}
}
}
}