Skip to content

2524. Maximum Frequency Score of a Subarray 👍

  • Time: $O(n)$
  • Space: $O(n)$
 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
class Solution {
 public:
  int maxFrequencyScore(vector<int>& nums, int k) {
    unordered_map<int, int> count;

    for (int i = 0; i < k; ++i)
      ++count[nums[i]];

    int sum = getInitialSum(count);
    int ans = sum;

    for (int i = k; i < nums.size(); ++i) {
      // Remove the leftmost number that's out-of-window.
      const int leftNum = nums[i - k];
      sum = (sum - modPow(leftNum, count[leftNum]) + kMod) % kMod;
      // After decreasing its frequency, if it's still > 0, then add it back.
      if (--count[leftNum] > 0)
        sum = (sum + modPow(leftNum, count[leftNum])) % kMod;
      // Otherwise, remove it from the count map.
      else
        count.erase(leftNum);
      // Add the current number. Similarly, remove the current score like above.
      const int rightNum = nums[i];
      if (count[rightNum] > 0)
        sum = (sum - modPow(rightNum, count[rightNum]) + kMod) % kMod;
      sum = (sum + modPow(rightNum, ++count[rightNum])) % kMod;
      ans = max(ans, sum);
    }

    return ans;
  }

 private:
  static constexpr int kMod = 1'000'000'007;

  int getInitialSum(const unordered_map<int, int>& count) {
    long sum = 0;
    for (const auto& [num, freq] : count)
      sum = (sum + modPow(num, freq)) % kMod;
    return sum;
  }

  long modPow(long x, long n) {
    if (n == 0)
      return 1;
    if (n % 2 == 1)
      return x * modPow(x % kMod, (n - 1)) % kMod;
    return modPow(x * x % kMod, (n / 2)) % kMod;
  }
};
 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
class Solution {
  public int maxFrequencyScore(int[] nums, int k) {
    Map<Integer, Integer> count = new HashMap<>();

    for (int i = 0; i < k; ++i)
      count.merge(nums[i], 1, Integer::sum);

    int sum = getInitialSum(count);
    int ans = sum;

    for (int i = k; i < nums.length; ++i) {
      // Remove the leftmost number that's out-of-window.
      final int leftNum = nums[i - k];
      sum = (sum - modPow(leftNum, count.get(leftNum)) + kMod) % kMod;
      // After decreasing its frequency, if it's still > 0, then add it back.
      if (count.merge(leftNum, -1, Integer::sum) > 0)
        sum = (sum + modPow(leftNum, count.get(leftNum))) % kMod;
      // Otherwise, remove it from the count map.
      else
        count.remove(leftNum);
      // Add the current number. Similarly, remove the current score like above.
      final int rightNum = nums[i];
      if (count.getOrDefault(rightNum, 0) > 0)
        sum = (sum - modPow(rightNum, count.get(rightNum)) + kMod) % kMod;
      sum = (sum + modPow(rightNum, count.merge(rightNum, 1, Integer::sum))) % kMod;
      ans = Math.max(ans, sum);
    }

    return ans;
  }

  private static final int kMod = 1_000_000_007;

  private int getInitialSum(Map<Integer, Integer> count) {
    int sum = 0;
    for (Map.Entry<Integer, Integer> entry : count.entrySet()) {
      final int num = entry.getKey();
      final int freq = entry.getValue();
      sum = (sum + modPow(num, freq)) % kMod;
    }
    return sum;
  }

  private int modPow(long x, long n) {
    if (n == 0)
      return 1;
    if (n % 2 == 1)
      return (int) (x * modPow(x % kMod, (n - 1)) % kMod);
    return modPow(x * x % kMod, (n / 2)) % kMod;
  }
}
 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
class Solution:
  def maxFrequencyScore(self, nums: List[int], k: int) -> int:
    kMod = 1_000_000_007
    count = collections.Counter(nums[:k])
    summ = self._getInitialSumm(count, kMod)
    ans = summ

    for i in range(k, len(nums)):
      # Remove the leftmost number that's out-of-window.
      leftNum = nums[i - k]
      summ = (summ - pow(leftNum, count[leftNum], kMod) + kMod) % kMod
      # After decreasing its frequency, if it's still > 0, then add it back.
      count[leftNum] -= 1
      if count[leftNum] > 0:
        summ = (summ + pow(leftNum, count[leftNum], kMod)) % kMod
      # Otherwise, remove it from the count map.
      else:
        del count[leftNum]
      # Add the current number. Similarly, remove the current score like above.
      rightNum = nums[i]
      if count[rightNum] > 0:
        summ = (summ - pow(rightNum, count[rightNum], kMod) + kMod) % kMod
      count[rightNum] += 1
      summ = (summ + pow(rightNum, count[rightNum], kMod)) % kMod
      ans = max(ans, summ)

    return ans

  def _getInitialSumm(self, count: Dict[int, int], kMod: int) -> int:
    summ = 0
    for num, freq in count.items():
      summ = (summ + pow(num, freq, kMod)) % kMod
    return summ