package com.fishercoder.solutions;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.PriorityQueue;

/**
 * 703. Kth Largest Element in a Stream
 *
 * Design a class to find the kth largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth distinct element.
 * Your KthLargest class will have a constructor which accepts an integer K and an integer array nums,
 * which contains initial elements from the stream.
 * For each call to the method KthLargest.add, return the element representing the kth largest element in the stream.
 *
 * Example:
 *
 * int K = 3;
 * int[] arr = [4,5,8,2];
 * KthLargest kthLargest = new KthLargest(3, arr);
 * kthLargest.add(3);   // returns 4
 * kthLargest.add(5);   // returns 5
 * kthLargest.add(10);  // returns 5
 * kthLargest.add(9);   // returns 8
 * kthLargest.add(4);   // returns 8
 * Note:
 * You may assume that nums' length ≥ K-1 and K ≥ 1.
 */
public class _703 {
  public static class Solution1 {
    public static class KthLargest {
      PriorityQueue<Integer> heap;
      int maxK;

      public KthLargest(int k, int[] nums) {
        heap = new PriorityQueue<>(Collections.reverseOrder());
        for (int num : nums) {
          heap.offer(num);
        }
        maxK = k;
      }

      public int add(int val) {
        List<Integer> tmp = new ArrayList<>();
        int result = 0;
        int tmpK = maxK;
        heap.offer(val);
        while (tmpK-- > 0) {
          result = heap.poll();
          tmp.add(result);
        }
        for (int num : tmp) {
          heap.offer(num);
        }
        return result;
      }
    }
  }
}

@@ -147,842 +147,8 @@
 e;%0A%0A
-/**%0A * 703. Kth Largest Element in a Stream%0A *%0A * Design a class to find the kth largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth distinct element.%0A * Your KthLargest class will have a constructor which accepts an integer K and an integer array nums,%0A * which contains initial elements from the stream.%0A * For each call to the method KthLargest.add, return the element representing the kth largest element in the stream.%0A *%0A * Example:%0A *%0A * int K = 3;%0A * int%5B%5D arr = %5B4,5,8,2%5D;%0A * KthLargest kthLargest = new KthLargest(3, arr);%0A * kthLargest.add(3);   // returns 4%0A * kthLargest.add(5);   // returns 5%0A * kthLargest.add(10);  // returns 5%0A * kthLargest.add(9);   // returns 8%0A * kthLargest.add(4);   // returns 8%0A * Note:%0A * You may assume that nums' length %E2%89%A5 K-1 and K %E2%89%A5 1.%0A */%0A
 publ
@@ -163,16 +163,18 @@
  _703 %7B%0A
+  
   public
@@ -199,24 +199,28 @@
 tion1 %7B%0A    
+  
+  
 public stati
@@ -246,16 +246,22 @@
 %7B%0A      
+  
+    
 Priority
@@ -283,24 +283,30 @@
 heap;%0A      
+    
+  
 int maxK;%0A%0A 
@@ -304,16 +304,22 @@
  maxK;%0A%0A
+      
       pu
@@ -347,32 +347,40 @@
 , int%5B%5D nums) %7B%0A
+        
         heap = n
@@ -427,16 +427,24 @@
 der());%0A
+        
         
@@ -468,32 +468,42 @@
 ms) %7B%0A          
+        
+  
 heap.offer(num);
@@ -511,26 +511,40 @@
         
-%7D%0A
+        %7D%0A      
         
 maxK = k
@@ -535,16 +535,18 @@
         
+  
 maxK = k
@@ -547,16 +547,22 @@
 xK = k;%0A
+      
       %7D%0A
@@ -562,16 +562,22 @@
      %7D%0A%0A
+      
       pu
@@ -608,16 +608,24 @@
         
+  
+      
 List%3CInt
@@ -651,24 +651,32 @@
 ayList%3C%3E();%0A
+        
         int 
@@ -691,24 +691,32 @@
  0;%0A        
+  
+      
 int tmpK = m
@@ -720,32 +720,40 @@
 = maxK;%0A        
+  
+      
 heap.offer(val);
@@ -761,16 +761,24 @@
         
+  
+      
 while (t
@@ -786,24 +786,34 @@
 pK-- %3E 0) %7B%0A
+          
           re
@@ -828,24 +828,34 @@
 eap.poll();%0A
+          
           tm
@@ -869,34 +869,50 @@
 esult);%0A        
+      
-%7D%0A
+  %7D%0A        
         for (int
@@ -927,32 +927,42 @@
 mp) %7B%0A          
+      
+    
 heap.offer(num);
@@ -970,26 +970,38 @@
         
-%7D%0A
+        %7D%0A    
         
 return r
@@ -992,16 +992,20 @@
         
+    
 return r
@@ -1021,16 +1021,28 @@
     
+    
-%7D%0A  
   %7D%0A
+        %7D%0A  
   %7D%0A

refactor 703