classSolution { public: ll countGood(vector<int>& nums, int k){ ll ans = 0; unordered_map<int, int> ma; ll now = 0; for (int l = 0, r = 0; r < nums.size(); r++) { now += ma[nums[r]]++; while (now >= k) { now -= --ma[nums[l++]]; } ans += l; } return ans; } };
''' Author: LetMeFly Date: 2025-04-16 20:23:21 LastEditors: LetMeFly.xyz LastEditTime: 2025-04-16 20:26:37 ''' from typing importList from collections import defaultdict
classSolution: defcountGood(self, nums: List[int], k: int) -> int: times = defaultdict(int) ans = l = now = 0 for t in nums: now += times[t] times[t] += 1 while now >= k: times[nums[l]] -= 1 now -= times[nums[l]] l += 1 ans += l return ans
funccountGood(nums []int, k int) (ans int64) { times := map[int]int64{} now := int64(0) for l, r := 0, 0; r < len(nums); r++ { now += times[nums[r]] times[nums[r]]++ for now >= int64(k) { times[nums[l]]-- now -= times[nums[l]] l++ } ans += int64(l) } return }