Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int atMostK(vector<int>& A, int K){
- int ans = 0, n=A.size(), right, left;
- unordered_map<int, int> count;
- for(left=right=0; right<n; right++){
- if(!count[A[right]]++) K--;
- while(K < 0){
- if(--count[A[left]] == 0) K++;
- left++;
- }
- ans += (right-left+1);
- }
- return ans;
- }
- int subarraysWithKDistinct(vector<int>& A, int K) {
- return atMostK(A, K) - atMostK(A, K-1);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement