forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maximum-frequency-score-of-a-subarray.cpp
82 lines (77 loc) · 2.62 KB
/
maximum-frequency-score-of-a-subarray.cpp
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
// Time: O(n)
// Space: O(n)
// two pointers, sliding window, freq table, hash table
class Solution {
public:
int maxFrequencyScore(vector<int>& nums, int k) {
static const int MOD = 1e9 + 7;
unordered_map<int, unordered_map<int, int>> lookup;
const auto& powmod = [&](int n, int p) {
if (!lookup.count(n) || !lookup[n].count(p)) {
lookup[n][p] = p >= 2 ? (static_cast<int64_t>(lookup[n][p - 1]) * n) % MOD : n % MOD; // assumed powmod(n, p-1) was accessed before powmod(n, p)
}
return lookup[n][p];
};
int result = 0, curr = 0;
unordered_map<int, int> cnt;
for (int i = 0; i < size(nums); ++i) {
if (i >= k) {
curr = ((curr - powmod(nums[i - k], cnt[nums[i - k]])) % MOD + MOD) % MOD;
--cnt[nums[i - k]];
if (cnt[nums[i - k]]) {
curr = (curr + powmod(nums[i - k], cnt[nums[i - k]])) % MOD;
}
}
if (cnt[nums[i]]) {
curr = ((curr - powmod(nums[i], cnt[nums[i]])) % MOD + MOD) % MOD;
}
++cnt[nums[i]];
curr = (curr + powmod(nums[i], cnt[nums[i]])) % MOD;
if (i >= k - 1) {
result = max(result, curr);
}
}
return result;
}
};
// Time: O(n)
// Space: O(n)
// two pointers, sliding window, freq table
class Solution2 {
public:
int maxFrequencyScore(vector<int>& nums, int k) {
static const int MOD = 1e9 + 7;
const auto& powmod = [&](int a, int b) {
a %= MOD;
int64_t result = 1;
while (b) {
if (b & 1) {
result = result * a % MOD;
}
a = int64_t(a) * a % MOD;
b >>= 1;
}
return result;
};
int result = 0, curr = 0;
unordered_map<int, int> cnt;
for (int i = 0; i < size(nums); ++i) {
if (i >= k) {
curr = ((curr - powmod(nums[i - k], cnt[nums[i - k]])) % MOD + MOD) % MOD;
--cnt[nums[i - k]];
if (cnt[nums[i - k]]) {
curr = (curr + powmod(nums[i - k], cnt[nums[i - k]])) % MOD;
}
}
if (cnt[nums[i]]) {
curr = ((curr - powmod(nums[i], cnt[nums[i]])) % MOD + MOD) % MOD;
}
++cnt[nums[i]];
curr = (curr + powmod(nums[i], cnt[nums[i]])) % MOD;
if (i >= k - 1) {
result = max(result, curr);
}
}
return result;
}
};