-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.java
47 lines (45 loc) · 1.4 KB
/
Solution.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
class Solution {
private final int mod = (int) 1e9 + 7;
public int maxFrequencyScore(int[] nums, int k) {
Map<Integer, Integer> cnt = new HashMap<>();
for (int i = 0; i < k; ++i) {
cnt.merge(nums[i], 1, Integer::sum);
}
long cur = 0;
for (var e : cnt.entrySet()) {
cur = (cur + qpow(e.getKey(), e.getValue())) % mod;
}
long ans = cur;
for (int i = k; i < nums.length; ++i) {
int a = nums[i - k];
int b = nums[i];
if (a != b) {
if (cnt.getOrDefault(b, 0) > 0) {
cur += (b - 1) * qpow(b, cnt.get(b)) % mod;
} else {
cur += b;
}
if (cnt.getOrDefault(a, 0) > 1) {
cur -= (a - 1) * qpow(a, cnt.get(a) - 1) % mod;
} else {
cur -= a;
}
cur = (cur + mod) % mod;
cnt.put(b, cnt.getOrDefault(b, 0) + 1);
cnt.put(a, cnt.getOrDefault(a, 0) - 1);
ans = Math.max(ans, cur);
}
}
return (int) ans;
}
private long qpow(long a, long n) {
long ans = 1;
for (; n > 0; n >>= 1) {
if ((n & 1) == 1) {
ans = ans * a % mod;
}
a = a * a % mod;
}
return ans;
}
}