YChalk

Medians

Mar 29th, 2022 (edited)
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.47 KB | None | 0 0
  1. import java.io.*;
  2. import java.math.*;
  3. import java.security.*;
  4. import java.text.*;
  5. import java.util.*;
  6. import java.util.concurrent.*;
  7. import java.util.function.*;
  8. import java.util.regex.*;
  9. import java.util.stream.*;
  10. import static java.util.stream.Collectors.joining;
  11. import static java.util.stream.Collectors.toList;
  12.  
  13. class Result {
  14.  
  15.     /*
  16.      * Complete the 'activityNotifications' function below.
  17.      *
  18.      * The function is expected to return an INTEGER.
  19.      * The function accepts following parameters:
  20.      *  1. INTEGER_ARRAY expenditure
  21.      *  2. INTEGER d
  22.      */
  23.  
  24.     public static int activityNotifications(List<Integer> expenditure, int d) {
  25.     // Write your code here
  26.     int result = 0;    
  27.     double median;
  28.     int remove = 0;
  29.     /*
  30.     *create 2 PriorityQueue
  31.     *minHeap contains smaller half of numbers
  32.     *maxHeap contains larger half of numbers
  33.     *populate them with the first d elements of expenditure
  34.     */
  35.     Queue<Integer> minHeap, maxHeap;
  36.    
  37.     minHeap = new PriorityQueue<>();
  38.     maxHeap = new PriorityQueue<>(Comparator.reverseOrder());
  39.    
  40.     for (int i = 0; i < d; i++) {
  41.        if (minHeap.size() == maxHeap.size()) {
  42.             maxHeap.offer(expenditure.get(i));
  43.             minHeap.offer(maxHeap.poll());
  44.         } else {
  45.             minHeap.offer(expenditure.get(i));
  46.             maxHeap.offer(minHeap.poll());
  47.         }
  48.     }
  49.    
  50.    
  51.     for (int i = d; i < expenditure.size(); i++){                  
  52.         /*
  53.         *find the median by either:
  54.         *getting the largest element of minHeap
  55.         *Or
  56.         *finding the sum of largest of minHeap and smallest of maxHeap and divide by 2
  57.         */
  58.         if (minHeap.size() > maxHeap.size()) {
  59.             median = minHeap.poll();
  60.         } else {
  61.             median = ((double) minHeap.poll() + maxHeap.poll()) / 2;
  62.         }
  63.        
  64.         if (expenditure.get(i) >= median * 2) result++;
  65.        
  66.         /*
  67.         *remove the "first" element from the relevant Queue
  68.         */
  69.         if (!minHeap.remove(expenditure.get(remove))) {
  70.             maxHeap.remove(expenditure.get(remove));
  71.         }
  72.        
  73.         remove++;
  74.        
  75.         /*
  76.         *add the next element to the relevant Queue
  77.         */
  78.         if (minHeap.size() == maxHeap.size()) {
  79.             maxHeap.offer(expenditure.get(i));
  80.             minHeap.offer(maxHeap.poll());
  81.         } else {
  82.             minHeap.offer(expenditure.get(i));
  83.             maxHeap.offer(minHeap.poll());
  84.         }
  85.     }
  86.    
  87.     return result;
  88.  
  89.     }
  90.  
  91. }
  92.  
  93. public class Solution {
  94.     public static void main(String[] args) throws IOException {
  95.         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
  96.         BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
  97.  
  98.         String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
  99.  
  100.         int n = Integer.parseInt(firstMultipleInput[0]);
  101.  
  102.         int d = Integer.parseInt(firstMultipleInput[1]);
  103.  
  104.         List<Integer> expenditure = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
  105.             .map(Integer::parseInt)
  106.             .collect(toList());
  107.  
  108.         int result = Result.activityNotifications(expenditure, d);
  109.  
  110.         bufferedWriter.write(String.valueOf(result));
  111.         bufferedWriter.newLine();
  112.  
  113.         bufferedReader.close();
  114.         bufferedWriter.close();
  115.     }
  116. }
  117.  
Add Comment
Please, Sign In to add comment