Description
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.length
Solutions
This is the textbook sliding window approach using a Map of frequency counts. Each step, the element at right enters the window and its count is incremented; if that count now exceeds k, the while loop shrinks the window from the left — decrementing the count of nums[left] and advancing left — until the offending value is back down to k occurrences. Because the window is guaranteed valid after every iteration, max = Math.max(max, right - left + 1) captures the longest subarray in which no value appears more than k times, and each element enters and leaves the window at most once, giving O(n) time overall.
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var maxSubarrayLength = function(nums, k) {
const map = new Map();
let left = 0;
let max = 0;
for (let right = 0; right < nums.length; right++) {
map.set(nums[right], (map.get(nums[right]) || 0) + 1);
while (map.get(nums[right]) > k) {
map.set(nums[left], map.get(nums[left]) - 1);
left++;
}
max = Math.max(max, right - left + 1);
}
return max;
};