forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maximum-frequency-score-of-a-subarray.py
67 lines (59 loc) · 1.99 KB
/
maximum-frequency-score-of-a-subarray.py
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
# Time: O(n)
# Space: O(n)
import collections
# two pointers, sliding window freq table, hash table
class Solution(object):
def maxFrequencyScore(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
MOD = 10**9+7
lookup = {}
def powmod(n, p):
if (n, p) not in lookup:
lookup[n, p] = (lookup[n, p-1]*n)%MOD if p >= 2 else n%MOD # assumed powmod(n, p-1) was accessed before powmod(n, p)
return lookup[n, p]
result = curr = 0
cnt = collections.Counter()
for i in xrange(len(nums)):
if i >= k:
curr = (curr-powmod(nums[i-k], cnt[nums[i-k]]))%MOD
cnt[nums[i-k]] -= 1
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
cnt[nums[i]] += 1
curr = (curr+powmod(nums[i], cnt[nums[i]]))%MOD
if i >= k-1:
result = max(result, curr)
return result
# Time: O(nlogn)
# Space: O(n)
import collections
# two pointers, sliding window, freq table
class Solution2(object):
def maxFrequencyScore(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
MOD = 10**9+7
result = curr = 0
cnt = collections.Counter()
for i in xrange(len(nums)):
if i >= k:
curr = (curr-pow(nums[i-k], cnt[nums[i-k]], MOD))%MOD
cnt[nums[i-k]] -= 1
if cnt[nums[i-k]]:
curr = (curr+pow(nums[i-k], cnt[nums[i-k]], MOD))%MOD
if cnt[nums[i]]:
curr = (curr-pow(nums[i], cnt[nums[i]], MOD))%MOD
cnt[nums[i]] += 1
curr = (curr+pow(nums[i], cnt[nums[i]], MOD))%MOD
if i >= k-1:
result = max(result, curr)
return result