# Top k Largest Numbers II

`Data Stream`, `Priority Queue`, `Heap`

Medium

## Question

Implement a data structure, provide two interfaces:

1. `add(number)`. Add a new number in the data structure.
2. `topk()`. Return the top `k` largest numbers in this data structure. `k` is given when we create the data structure.

Example

Example1

``````s = new Solution(3);
>> create a new data structure.
s.topk()
>> return [10, 3]
s.topk()
>> return [1000, 10, 3]
s.topk()
>> return [1000, 10, 4]
s.topk()
>> return [1000, 100, 10]
``````

Example2

``````Input:
s = new Solution(1);
s.topk()
s.topk()

Output:
[10]
[10]

Explanation:
s = new Solution(1);
>> create a new data structure, and k = 1.
s.topk()
>> return [10]
s.topk()
>> return [10]
``````

## Solution

#### Min-Heap + iterator()

``````public class Solution {
private PriorityQueue<Integer> minheap;
private int maxSize;

public Solution(int k) {
minheap = new PriorityQueue<Integer>();
maxSize = k;
}

public void add(int num) {
if (minheap.size() < maxSize) {
minheap.offer(num);
} else {
if (num > minheap.peek()) {
minheap.poll();
minheap.offer(num);
}
}

}

public List<Integer> topk() {
Iterator iter = minheap.iterator();
List<Integer> result = new ArrayList<Integer>();
while (iter.hasNext()) {