# Top k Largest Numbers II

## Problem

### Description

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

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]


## 题解

### Java

public class Solution {
private int k = -1;
private Queue<Integer> heap = null;
/*
* @param k: An integer
*/public Solution(int k) {
// do intialization if necessary
this.k = k;
heap = new PriorityQueue<Integer>(k);
}

/*
* @param num: Number to be added
* @return: nothing
*/
if (heap.size() < k) {
heap.offer(num);
} else if (heap.peek() < num) {
heap.poll();
heap.offer(num);
}
}

/*
* @return: Top k element
*/
public List<Integer> topk() {
List<Integer> result = new ArrayList<>(k);
Iterator<Integer> it = heap.iterator();
while(it.hasNext()) {
}
result.sort(Collections.reverseOrder());
return result;
}
}