返回介绍

solution / 2500-2599 / 2524.Maximum Frequency Score of a Subarray / README_EN

发布于 2024-06-17 01:03:04 字数 5848 浏览 0 评论 0 收藏 0

2524. Maximum Frequency Score of a Subarray

中文文档

Description

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 modulo 109 + 7.

  • For example, the frequency score of the array [5,4,5,7,4,4] is (43 + 52 + 71) modulo (109 + 7) = 96.

Return _the maximum frequency score of a subarray of size _k_ in _nums. You should maximize the value under the modulo and not the actual value.

A subarray is a contiguous part of an array.

 

Example 1:

Input: nums = [1,1,1,2,1,2], k = 3
Output: 5
Explanation: 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:

Input: nums = [1,1,1,1,1,1], k = 4
Output: 1
Explanation: All the subarrays of length 4 have a frequency score equal to 1.

 

Constraints:

  • 1 <= k <= nums.length <= 105
  • 1 <= nums[i] <= 106

Solutions

Solution 1

class Solution:
  def maxFrequencyScore(self, nums: List[int], k: int) -> int:
    mod = 10**9 + 7
    cnt = Counter(nums[:k])
    ans = cur = sum(pow(k, v, mod) for k, v in cnt.items()) % mod
    i = k
    while i < len(nums):
      a, b = nums[i - k], nums[i]
      if a != b:
        cur += (b - 1) * pow(b, cnt[b], mod) if cnt[b] else b
        cur -= (a - 1) * pow(a, cnt[a] - 1, mod) if cnt[a] > 1 else a
        cur %= mod
        cnt[b] += 1
        cnt[a] -= 1
        ans = max(ans, cur)
      i += 1
    return ans
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;
  }
}
class Solution {
public:
  int maxFrequencyScore(vector<int>& nums, int k) {
    using ll = long long;
    const int mod = 1e9 + 7;
    auto qpow = [&](ll a, ll n) {
      ll ans = 1;
      for (; n; n >>= 1) {
        if (n & 1) {
          ans = ans * a % mod;
        }
        a = a * a % mod;
      }
      return ans;
    };
    unordered_map<int, int> cnt;
    for (int i = 0; i < k; ++i) {
      cnt[nums[i]]++;
    }
    ll cur = 0;
    for (auto& [k, v] : cnt) {
      cur = (cur + qpow(k, v)) % mod;
    }
    ll ans = cur;
    for (int i = k; i < nums.size(); ++i) {
      int a = nums[i - k], b = nums[i];
      if (a != b) {
        cur += cnt[b] ? (b - 1) * qpow(b, cnt[b]) % mod : b;
        cur -= cnt[a] > 1 ? (a - 1) * qpow(a, cnt[a] - 1) % mod : a;
        cur = (cur + mod) % mod;
        ans = max(ans, cur);
        cnt[b]++;
        cnt[a]--;
      }
    }
    return ans;
  }
};
func maxFrequencyScore(nums []int, k int) int {
  cnt := map[int]int{}
  for _, v := range nums[:k] {
    cnt[v]++
  }
  cur := 0
  const mod int = 1e9 + 7
  qpow := func(a, n int) int {
    ans := 1
    for ; n > 0; n >>= 1 {
      if n&1 == 1 {
        ans = ans * a % mod
      }
      a = a * a % mod
    }
    return ans
  }
  for k, v := range cnt {
    cur = (cur + qpow(k, v)) % mod
  }
  ans := cur
  for i := k; i < len(nums); i++ {
    a, b := nums[i-k], nums[i]
    if a != b {
      if cnt[b] > 0 {
        cur += (b - 1) * qpow(b, cnt[b]) % mod
      } else {
        cur += b
      }
      if cnt[a] > 1 {
        cur -= (a - 1) * qpow(a, cnt[a]-1) % mod
      } else {
        cur -= a
      }
      cur = (cur + mod) % mod
      ans = max(ans, cur)
      cnt[b]++
      cnt[a]--
    }
  }
  return ans
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文