Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- using ll = long long;
- public:
- long long countGood(vector<int>& nums, int k) {
- int n = nums.size();
- ll ans = 0;
- ll p = 0;
- map<int, int> cnt;
- for (int i = 0, j = 0; i < n; ++i) {
- while(j < n && p < k) {
- p += cnt[nums[j]];
- cnt[nums[j]]++;
- j++;
- }
- // cout << "i: " << i << " j: " << j << " n - j: " << n - j << endl;
- if (p >= k) ans += n - j + 1;
- cnt[nums[i]]--;
- p -= cnt[nums[i]];
- }
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement