2958.cpp (431B)
1 class Solution { 2 public: 3 int maxSubarrayLength(const vector<int> &nums, const int k) const { 4 unordered_map<int, int> freq; 5 const int n = size(nums); 6 int res = 0, i = 0; 7 8 for (int j = 0; j < n; j++) { 9 freq[nums[j]]++; 10 while (freq[nums[j]] > k) 11 freq[nums[i++]]--; 12 res = max(res, j - i + 1); 13 } 14 15 return max(res, n - i); 16 } 17 };