-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_3318.java
59 lines (53 loc) · 1.91 KB
/
_3318.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.fishercoder.solutions.fourththousand;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
public class _3318 {
public static class Solution1 {
public int[] findXSum(int[] nums, int k, int x) {
PriorityQueue<int[]> maxHeap =
new PriorityQueue<>(
(a, b) ->
a[1] != b[1]
? b[1] - a[1]
: b[0] - a[0]); // a[0] is the number itself, a[1]
// is the frequency
Map<Integer, int[]> map = new HashMap<>();
int i = 0;
for (; i < k; i++) {
int[] a = map.getOrDefault(nums[i], new int[2]);
a[0] = nums[i];
a[1]++;
map.put(nums[i], a);
}
maxHeap.addAll(map.values());
int[] ans = new int[nums.length - k + 1];
for (int j = i - 1, p = 0; j < nums.length; ) {
ans[p++] = computeTopX(new PriorityQueue<>(maxHeap), x);
j++;
if (j >= nums.length) {
break;
}
int[] a = map.getOrDefault(nums[j], new int[2]);
a[0] = nums[j];
a[1]++;
map.put(nums[j], a);
a = map.getOrDefault(nums[j - k], new int[2]);
a[0] = nums[j - k];
a[1]--;
map.put(nums[j - k], a);
maxHeap.clear();
maxHeap.addAll(map.values());
}
return ans;
}
private int computeTopX(PriorityQueue<int[]> maxHeap, int x) {
int sum = 0;
while (!maxHeap.isEmpty() && x-- > 0) {
int[] a = maxHeap.poll();
sum += a[0] * a[1];
}
return sum;
}
}
}