1. class KthLargest {
    2. final private PriorityQueue<Integer> queue;
    3. final int k;
    4. public KthLargest(int k, int[] nums) {
    5. queue = new PriorityQueu e(k);
    6. this.k = k;
    7. for (int num : nums) {
    8. add(num);
    9. }
    10. }
    11. public int add(int val) {
    12. if (queue.size() < k) {
    13. queue.offer(val);
    14. } else if (queue.peek() < val) {
    15. queue.poll();
    16. queue.offer(val);
    17. }
    18. return queue.peek();
    19. }
    20. // public int add(int val) {
    21. // queue.offer(val);
    22. // if (queue.size() > k) {
    23. // queue.poll();
    24. // }
    25. // return queue.peek();
    26. // }
    27. }