2958. Length of Longest Subarray With at Most K Frequency

Overview
Find the longest subarray where each number can occur with a frequency of at most k.
Solution 1: Sliding Window
Thinking
This solution is really similar to what we did yesterday.
Algorithm
-
Initialize
left
to zero, it will point on the head of thenums
array.
Initialize acounter
dictionary to count down the number and its frequency.
Initializemax_length
to store the longest subarray length. -
Iterate from 0 to the length of the
nums
array asright
.- Set
nums[right]
asn
. - Increase
counter[n]
by 1. - If
counter[n]
is greater thank
, indicating it does not satisfy the limitation:- Decrease
counter[nums[left]]
by 1. - Increase
left
by 1 to point to the next number.
- Decrease
- Update
max_length
when theright - left + 1
is greater than the originalmax_length
.
- Set
-
Return the
max_length
as the answer.
Implement
class Solution: def maxSubarrayLength(self, nums: list[int], k: int) -> int: left = 0 counter: dict[int, int] = {} max_length = 0 for right in range(len(nums)): n = nums[right] counter[n] = counter.get(n, 0) + 1 while counter[n] > k: counter[nums[left]] -= 1 left += 1 max_length = max(max_length, right - left + 1) return max_length
Complexity Analysis
Time complexity: O(n)
Iterating from 0 to the length of the nums
array takes O(n).
And we perform a while loop, moving the left pointer to the right.
Each number will be operated on at most twice.
The total time complexity might be O(2n), simplifies to O(n).
Space complexity: O(n)
We use an additional dictionary to count down the number and its frequency, taking O(n) space.