-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleet-code-q-70.java
More file actions
84 lines (79 loc) · 2.37 KB
/
leet-code-q-70.java
File metadata and controls
84 lines (79 loc) · 2.37 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import java.util.*;
class Solution {
private static final class Pair {
final int f, v;
Pair(int f, int v){ this.f = f; this.v = v; }
@Override public boolean equals(Object o){
if (this == o) return true;
if (!(o instanceof Pair)) return false;
Pair p = (Pair)o; return f == p.f && v == p.v;
}
@Override public int hashCode(){ return Objects.hash(f, v); }
}
private static final Comparator<Pair> DESC = (a, b) -> {
if (a.f != b.f) return Integer.compare(b.f, a.f);
return Integer.compare(b.v, a.v);
};
private Map<Integer,Integer> cnt;
private TreeSet<Pair> top, rest;
private long topSum;
private void pull(int v, int f){
Pair key = new Pair(f, v);
if (top.remove(key)) {
topSum -= 1L * f * v;
} else {
rest.remove(key);
}
}
private void pushToTop(int v, int f){
top.add(new Pair(f, v));
topSum += 1L * f * v;
}
private void insertVal(int v, int x){
int f = cnt.getOrDefault(v, 0);
if (f > 0) pull(v, f);
f += 1;
cnt.put(v, f);
pushToTop(v, f);
if (top.size() > x){
Pair worst = top.last();
top.remove(worst);
topSum -= 1L * worst.f * worst.v;
rest.add(worst);
}
}
private void eraseVal(int v, int x){
Integer F = cnt.get(v);
if (F == null || F == 0) return;
int f = F;
pull(v, f);
f -= 1;
if (f == 0) cnt.remove(v);
else {
cnt.put(v, f);
rest.add(new Pair(f, v));
}
if (top.size() < x && !rest.isEmpty()){
Pair best = rest.first();
rest.remove(best);
top.add(best);
topSum += 1L * best.f * best.v;
}
}
public long[] findXSum(int[] nums, int k, int x) {
int n = nums.length;
long[] ans = new long[n - k + 1];
cnt = new HashMap<>(Math.max(16, n * 2));
top = new TreeSet<>(DESC);
rest = new TreeSet<>(DESC);
topSum = 0;
for (int i = 0; i < k; ++i) insertVal(nums[i], x);
ans[0] = topSum;
for (int i = k; i < n; ++i){
eraseVal(nums[i - k], x);
insertVal(nums[i], x);
ans[i - k + 1] = topSum;
}
return ans;
}
}