Talentd
FleetCode

2524. Maximum Frequency Score of a Subarray

Hard35.7% acceptance1 companyPremium elsewhere, free here

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

Asked at 1 company

Hints open one at a time

Hint 1
Calculate the frequency score of each subarray of size k and return the maximum one.
Hint 2
Use the sliding window technique to keep the frequency scores.
11:51
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def solve(self):
3 pass
4
Starter codePython 3Spaces: 4
Examples
Input
[1,1,1,2,1,2]
3
Input
[1,1,1,1,1,1]
4
Your code is saved on this device.Solution