You are given an integer array nums and an integer k.
The frequency of an element x is the number of times it occurs in an array.
An array is called good if the frequency of each element in this array is less than or equal to k.
Return the length of the longest good subarray of nums.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [1,2,3,1,2,3,1,2], k = 2 Output: 6 Explanation: The longest possible good subarray is [1,2,3,1,2,3] since the values 1, 2, and 3 occur at most twice in this subarray. Note that the subarrays [2,3,1,2,3,1] and [3,1,2,3,1,2] are also good. It can be shown that there are no good subarrays with length more than 6.
Example 2:
Input: nums = [1,2,1,2,1,2,1,2], k = 1 Output: 2 Explanation: The longest possible good subarray is [1,2] since the values 1 and 2 occur at most once in this subarray. Note that the subarray [2,1] is also good. It can be shown that there are no good subarrays with length more than 2.
Example 3:
Input: nums = [5,5,5,5,5,5,5], k = 4 Output: 4 Explanation: The longest possible good subarray is [5,5,5,5] since the value 5 occurs 4 times in this subarray. It can be shown that there are no good subarrays with length more than 4.
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 1091 <= k <= nums.lengthWhen 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:
To find the longest section where no value appears too many times, the brute force method looks at every possible section. It checks each section to see if it meets the criteria, keeping track of the longest one that does.
Here's how the algorithm would work step-by-step:
def find_longest_subarray_brute_force(numbers, max_frequency):
longest_subarray_length = 0
for start_index in range(len(numbers)):
for end_index in range(start_index, len(numbers)):
subarray = numbers[start_index:end_index + 1]
# Count occurrences of each number in the current subarray
number_counts = {}
for number in subarray:
if number in number_counts:
number_counts[number] += 1
else:
number_counts[number] = 1
is_valid = True
# Check if any number appears more than max_frequency times
for number in number_counts:
if number_counts[number] > max_frequency:
is_valid = False
break
# Update longest subarray length if needed
if is_valid:
current_subarray_length = len(subarray)
# Keep track of the longest valid subarray
if current_subarray_length > longest_subarray_length:
longest_subarray_length = current_subarray_length
return longest_subarray_lengthThe goal is to find the longest continuous section where no value appears too many times. The clever trick is to use a 'sliding window' approach: think of it like looking at the data through a rectangle that expands or contracts as needed.
Here's how the algorithm would work step-by-step:
def longest_subarray_with_k_frequency(numbers, max_frequency):
window_start = 0
max_length = 0
frequency_map = {}
for window_end in range(len(numbers)):
right_number = numbers[window_end]
if right_number not in frequency_map:
frequency_map[right_number] = 0
frequency_map[right_number] += 1
# Shrink the window if any element exceeds max_frequency
while any(count > max_frequency for count in frequency_map.values()):
left_number = numbers[window_start]
# Decrement the frequency of the element leaving the window
frequency_map[left_number] -= 1
window_start += 1
# Remove the element if its frequency becomes 0
if frequency_map[left_number] == 0:
del frequency_map[left_number]
# Update the maximum length of the valid subarray
max_length = max(max_length, window_end - window_start + 1)
return max_length| Case | How to Handle |
|---|---|
| Empty array input | Return 0, as there's no subarray. |
| k = 0 and array has repeated elements | The longest subarray will be of length 1, return 1. |
| Array with all identical elements and k is less than the frequency of those elements | Return the length of the longest subarray with frequency limited by k, which equals k. |
| Array with all identical elements and k is greater than or equal to the frequency of those elements | Return the length of the array, as all elements satisfy the condition. |
| Array with a single element. | Return 1, as the subarray consisting of only that element is the longest. |
| Large array with k being a large number | Solution should efficiently track frequencies, avoiding brute-force recalculations with each window slide. |
| Array with negative numbers. | Hash map used to store element frequencies should correctly handle negative numbers as keys. |
| k is larger than array length | Return the length of the array, as all elements can be included. |