package leetcode;

/**
 * https://leetcode.com/problems/kth-largest-element-in-a-stream/
 */
public class Problem703 {
    private static class KthLargest {

        public KthLargest(int k, int[] nums) {
            // TODO
        }

        public int add(int val) {
            // TODO
            return 0;
        }
    }

    public static void main(String[] args) {
        int k = 3;
        int[] arr = new int[]{4, 5, 8, 2};
        KthLargest kthLargest = new KthLargest(3, arr);
        System.out.println(kthLargest.add(3)); // returns 4
        System.out.println(kthLargest.add(5)); // returns 5
        System.out.println(kthLargest.add(10)); // returns 5
        System.out.println(kthLargest.add(9)); // returns 8
        System.out.println(kthLargest.add(4)); // returns 8
    }
}

@@ -12,16 +12,49 @@
 tcode;%0A%0A
+import java.util.PriorityQueue;%0A%0A
 /**%0A * h
@@ -187,171 +187,302 @@
 t %7B%0A
-%0A        public KthLargest(int k, int%5B%5D nums) %7B%0A            // TODO%0A        %7D%0A%0A        public int add(int val) %7B%0A            // TODO%0A            return 0;%0A
+        private final PriorityQueue%3CInteger%3E queue = new PriorityQueue%3C%3E();%0A        private final int k;%0A%0A        public KthLargest(int k, int%5B%5D nums) %7B%0A            for (int i = 0; i %3C nums.length; i++) %7B%0A                queue.add(nums%5Bi%5D);%0A                if (queue.size() %3E k) %7B%0A     
         
 %7D%0A  
@@ -481,132 +481,72 @@
     
-%7D%0A
+ 
     
-%7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        int k = 3;%0A        int%5B%5D arr = new int%5B%5D%7B4, 5, 8, 2%7D;%0A
+  queue.poll();%0A                %7D%0A            %7D%0A   
         
 KthL
@@ -545,356 +545,216 @@
     
-KthLargest kthLargest = new KthLargest(3, arr);%0A        System.out.println(kthLargest.add(3)); // returns 4%0A        System.out.println(kthLargest.add(5)); // returns 5%0A        System.out.println(kthLargest.add(10)); // returns 5%0A        System.out.println(kthLargest.add(9)); // returns 8%0A        System.out.println(kthLargest.add(4)); // returns 8
+ this.k = k;%0A        %7D%0A%0A        public int add(int val) %7B%0A            queue.add(val);%0A            if (queue.size() %3E k) %7B%0A                queue.poll();%0A            %7D%0A            return queue.peek();%0A        %7D
 %0A   

Solve problem 703