Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.math.*;
- import java.security.*;
- import java.text.*;
- import java.util.*;
- import java.util.concurrent.*;
- import java.util.function.*;
- import java.util.regex.*;
- import java.util.stream.*;
- import static java.util.stream.Collectors.joining;
- import static java.util.stream.Collectors.toList;
- class Result {
- /*
- * Complete the 'activityNotifications' function below.
- *
- * The function is expected to return an INTEGER.
- * The function accepts following parameters:
- * 1. INTEGER_ARRAY expenditure
- * 2. INTEGER d
- */
- public static int activityNotifications(List<Integer> expenditure, int d) {
- // Write your code here
- int result = 0;
- double median;
- int remove = 0;
- /*
- *create 2 PriorityQueue
- *minHeap contains smaller half of numbers
- *maxHeap contains larger half of numbers
- *populate them with the first d elements of expenditure
- */
- Queue<Integer> minHeap, maxHeap;
- minHeap = new PriorityQueue<>();
- maxHeap = new PriorityQueue<>(Comparator.reverseOrder());
- for (int i = 0; i < d; i++) {
- if (minHeap.size() == maxHeap.size()) {
- maxHeap.offer(expenditure.get(i));
- minHeap.offer(maxHeap.poll());
- } else {
- minHeap.offer(expenditure.get(i));
- maxHeap.offer(minHeap.poll());
- }
- }
- for (int i = d; i < expenditure.size(); i++){
- /*
- *find the median by either:
- *getting the largest element of minHeap
- *Or
- *finding the sum of largest of minHeap and smallest of maxHeap and divide by 2
- */
- if (minHeap.size() > maxHeap.size()) {
- median = minHeap.poll();
- } else {
- median = ((double) minHeap.poll() + maxHeap.poll()) / 2;
- }
- if (expenditure.get(i) >= median * 2) result++;
- /*
- *remove the "first" element from the relevant Queue
- */
- if (!minHeap.remove(expenditure.get(remove))) {
- maxHeap.remove(expenditure.get(remove));
- }
- remove++;
- /*
- *add the next element to the relevant Queue
- */
- if (minHeap.size() == maxHeap.size()) {
- maxHeap.offer(expenditure.get(i));
- minHeap.offer(maxHeap.poll());
- } else {
- minHeap.offer(expenditure.get(i));
- maxHeap.offer(minHeap.poll());
- }
- }
- return result;
- }
- }
- public class Solution {
- public static void main(String[] args) throws IOException {
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
- BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
- String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
- int n = Integer.parseInt(firstMultipleInput[0]);
- int d = Integer.parseInt(firstMultipleInput[1]);
- List<Integer> expenditure = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
- .map(Integer::parseInt)
- .collect(toList());
- int result = Result.activityNotifications(expenditure, d);
- bufferedWriter.write(String.valueOf(result));
- bufferedWriter.newLine();
- bufferedReader.close();
- bufferedWriter.close();
- }
- }
Add Comment
Please, Sign In to add comment