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