Advertisement
pb_jiang

LC-WC T3 AC bi-pointer 22 lines

Jan 14th, 2023
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.61 KB | None | 0 0
  1. class Solution {
  2.     using ll = long long;
  3. public:
  4.     long long countGood(vector<int>& nums, int k) {
  5.         int n = nums.size();
  6.         ll ans = 0;
  7.         ll p = 0;
  8.         map<int, int> cnt;
  9.         for (int i = 0, j = 0; i < n; ++i) {
  10.             while(j < n && p < k) {
  11.                 p += cnt[nums[j]];
  12.                 cnt[nums[j]]++;
  13.                 j++;
  14.             }
  15.             // cout << "i: " << i << " j: " << j << " n - j: " << n - j << endl;
  16.             if (p >= k) ans += n - j + 1;
  17.             cnt[nums[i]]--;
  18.             p -= cnt[nums[i]];
  19.         }
  20.         return ans;
  21.     }
  22. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement