You are given an integer array nums and a positive integer k.
The frequency score of an array is the sum of the distinct values in the array raised to the power of their frequencies , taking the sum
modulo109 + 7.
For example, the frequency score of the array [5,4,5,7,4,4] is (43 + 52 + 71) modulo (109 + 7) = 96.
Return _themaximum frequency score of a subarray of size _kinnums. You should maximize the value under the modulo and not the actual value.
Input: nums =[1,1,1,2,1,2], k =3Output: 5Explanation: The subarray [2,1,2] has a frequency score equal to 5. It can be shown that it is the maximum frequency score we can have.
Example 2:
1
2
3
Input: nums =[1,1,1,1,1,1], k =4Output: 1Explanation: All the subarrays of length 4 have a frequency score equal to 1.
We want to efficiently compute the frequency score for every subarray of size k. By using a sliding window and a hash map to track frequencies, we can update the score incrementally as the window moves.
classSolution {
public:int maxFrequencyScore(vector<int>& nums, int k) {
constint MOD =1e9+7;
unordered_map<int, int> freq;
int n = nums.size();
longlong score =0, ans =0;
for (int i =0; i < n; ++i) {
freq[nums[i]]++;
if (i >= k) {
freq[nums[i-k]]--;
if (freq[nums[i-k]] ==0) freq.erase(nums[i-k]);
}
if (i >= k-1) {
longlong cur =0;
for (auto& [v, f] : freq) {
longlong t =1;
for (int j =0; j < f; ++j) t = t * v % MOD;
cur = (cur + t) % MOD;
}
ans = max(ans, cur);
}
}
return ans;
}
};
classSolution {
publicintmaxFrequencyScore(int[] nums, int k) {
int mod = 1000000007, n = nums.length, ans = 0;
Map<Integer, Integer> freq =new HashMap<>();
for (int i = 0; i < n; i++) {
freq.put(nums[i], freq.getOrDefault(nums[i], 0) + 1);
if (i >= k) {
freq.put(nums[i-k], freq.get(nums[i-k]) - 1);
if (freq.get(nums[i-k]) == 0) freq.remove(nums[i-k]);
}
if (i >= k-1) {
int cur = 0;
for (var e : freq.entrySet()) {
long t = 1;
for (int j = 0; j < e.getValue(); j++) t = t * e.getKey() % mod;
cur = (int)((cur + t) % mod);
}
ans = Math.max(ans, cur);
}
}
return ans;
}
}
classSolution {
funmaxFrequencyScore(nums: IntArray, k: Int): Int {
val mod = 1_000_000_007
val freq = mutableMapOf<Int, Int>()
var ans = 0for (i in nums.indices) {
freq[nums[i]] = freq.getOrDefault(nums[i], 0) + 1if (i >= k) {
freq[nums[i-k]] = freq.getOrDefault(nums[i-k], 0) - 1if (freq[nums[i-k]] ==0) freq.remove(nums[i-k])
}
if (i >= k-1) {
var cur = 0Lfor ((v, f) in freq) {
var t = 1L repeat(f) { t = t * v % mod }
cur = (cur + t) % mod
}
ans = maxOf(ans, cur.toInt())
}
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution:
defmaxFrequencyScore(self, nums: list[int], k: int) -> int:
from collections import defaultdict
mod =10**9+7 freq = defaultdict(int)
ans =0for i, v in enumerate(nums):
freq[v] +=1if i >= k:
freq[nums[i-k]] -=1if freq[nums[i-k]] ==0:
del freq[nums[i-k]]
if i >= k-1:
cur =0for val, f in freq.items():
t = pow(val, f, mod)
cur = (cur + t) % mod
ans = max(ans, cur)
return ans
impl Solution {
pubfnmax_frequency_score(nums: Vec<i32>, k: i32) -> i32 {
use std::collections::HashMap;
let modn =1_000_000_007;
letmut freq = HashMap::new();
letmut ans =0;
for (i, &v) in nums.iter().enumerate() {
*freq.entry(v).or_insert(0) +=1;
if i asi32>= k {
let e = freq.get_mut(&nums[i - k asusize]).unwrap();
*e -=1;
if*e ==0 { freq.remove(&nums[i - k asusize]); }
}
if i asi32>= k -1 {
letmut cur =0;
for (&val, &f) in&freq {
letmut t =1i64;
for _ in0..f {
t = t * val asi64% modn;
}
cur = (cur + t) % modn;
}
ans = ans.max(cur asi32);
}
}
ans
}
}