..
2958. Length of Longest Subarray With at Most K Frequency
Problem Description
You are given an integer array nums
and an integer k
.
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.
The frequency of an element
x
is the number of times it occurs in an array.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 <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length
Analysis
This problem involves finding the length of the longest subarray with a constraint on the frequency of each element.
Key aspects to consider in our analysis:
- Sliding Window Technique: We’ll use the sliding window technique to efficiently find the length of the longest subarray with at most
k
frequency for each element. - Maintaining Frequency Count: We’ll maintain a dictionary or hashmap to keep track of the frequency count of each element within the sliding window.
- Dynamic Window Size: As we traverse the array, we’ll dynamically adjust the size of the sliding window to ensure that the frequency constraint is met.
Solution
Here’s the provided Python solution:
class Solution:
def maxSubarrayLength(self, nums: List[int], k: int) -> int:
counter = {}
left = 0
max_len = 0
for right in range(len(nums)):
counter[nums[right]] = counter.get(nums[right], 0) + 1
if counter[nums[right]] > k:
while left < len(nums) and nums[left] != nums[right]:
counter[nums[left]] -= 1
left += 1
if left < len(nums) and nums[left] == nums[right]:
counter[nums[left]] -= 1
left += 1
max_len = max(max_len, right - left + 1)
return max_len
Complexity Analysis
Before delving into the solution, let’s understand the time complexity we aim to achieve:
- Time Complexity:
O(n)
- We need to traverse the input array once to find the length of the longest subarray. - Space Complexity:
O(1)
- We should not use any additional space proportional to the input array size, except for a few variables.