You are given a 0-indexed integer array nums
.
The frequency score of a subarray nums[l...r]
is the frequency of the most frequent element in the subarray.
Return the maximum frequency score of any subarray of nums
.
A subarray is a contiguous part of an array.
Example 1:
Input: nums = [1,2,3,3,3,1,4] Output: 3 Explanation: The subarray [3,3,3] has a frequency score of 3 since 3 occurs 3 times. It can be shown that no other subarray has a larger frequency score.
Example 2:
Input: nums = [1,2,1,2,1] Output: 3 Explanation: The subarray [1,2,1,2,1] has a frequency score of 3 since 1 occurs 3 times. It can be shown that no other subarray has a larger frequency score.
Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 105
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force strategy is all about trying every single possibility. For this problem, we examine every single group of consecutive numbers within the larger set of numbers to find one that matches certain conditions.
Here's how the algorithm would work step-by-step:
def maximum_frequency_score_brute_force(numbers):
maximum_frequency = 0
for start_index in range(len(numbers)):
for end_index in range(start_index, len(numbers)):
# Consider each subarray
subarray = numbers[start_index:end_index + 1]
if not subarray:
continue
frequency_map = {}
for number in subarray:
frequency_map[number] = frequency_map.get(number, 0) + 1
# Determine the maximum frequency of any number within the subarray
current_max_frequency = 0
for number in frequency_map:
current_max_frequency = max(current_max_frequency, frequency_map[number])
# Keep track of the largest maximum frequency seen so far.
maximum_frequency = max(maximum_frequency, current_max_frequency)
return maximum_frequency
The key is to efficiently track how often each number appears within a sliding window. Instead of recalculating frequencies from scratch for every possible section, we'll update them as we move the window, letting us quickly find the most frequent number and its count.
Here's how the algorithm would work step-by-step:
def maximum_frequency_score(numbers):
maximum_score = 0
for window_start in range(len(numbers)):
frequency_map = {}
current_maximum_frequency = 0
for window_end in range(window_start, len(numbers)):
right_element = numbers[window_end]
#Update the frequency of the current element.
if right_element in frequency_map:
frequency_map[right_element] += 1
else:
frequency_map[right_element] = 1
#Find the most frequent element.
current_maximum_frequency = max(frequency_map.values(), default = 0)
#Update the overall maximum score.
maximum_score = max(maximum_score, current_maximum_frequency)
return maximum_score
Case | How to Handle |
---|---|
Empty input array (nums is null or has length 0) | Return 0, as there are no subarrays to consider. |
Array with a single element | Return 1, as the score is 1 for that single element subarray. |
Array with all identical values (e.g., [5, 5, 5, 5]) | The frequency score will simply be the length of the array, as each element contributes 1 to its own score. |
Array with highly skewed distribution (e.g., [1, 1, 1, 1, 1000]) | The algorithm should correctly calculate frequencies even with outlier values. |
Array containing only negative numbers (e.g., [-1, -2, -3]) | The algorithm should work correctly with negative numbers without modification. |
Array containing zero (e.g., [0, 1, 2]) | Zero values should be treated the same as any other number when calculating frequencies. |
Large array size (e.g., nums.length close to Integer.MAX_VALUE) | The space complexity of frequency counters (if used) might become an issue; optimize for space or use a more efficient data structure if necessary. |
Integer overflow in frequency counts (if numbers repeat many times) | Use long data type for frequency counts to avoid potential overflow issues. |