/
subarrayKDifferentIntegers.cpp
51 lines (43 loc) · 1.48 KB
/
subarrayKDifferentIntegers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <vector>
#include <unordered_map>
#include <iostream>
using namespace std;
class Solution {
public:
int subarraysWithKDistinct(vector<int>& A, int K) {
int slow =0;
int ans = 0;
//while slow < size - K
while(slow<A.size()){
bool firstpass=true;
//initialise map, initialise fast, unique elems
unordered_map<int,int> db; int fast = slow; int uniqueElems = 0;
// while unique elems <= K && fast < size
while(uniqueElems<=K && fast < A.size()){
// if( unique elems < K){ increment fast; is item in hash map ? increment in hash map : incrememnt unique elems, increment in hash map; continue
if(uniqueElems<K){
if(!db[A[fast]]){
uniqueElems++;db[A[fast]]=1;
if(uniqueElems!=K){fast++;continue;
}}
else{
db[A[fast]]++;fast++;continue;}
}
//}
// if( A[fast] is in hash map) ans++;increment fast
if(db[A[fast]]){ans++;fast++;}
//else break;
else break;
}
//increment slow
slow++;
}
return ans;
}
};
int main(){
Solution s;
vector<int> in={1,2,1,2,3};
cout<<s.subarraysWithKDistinct(in,2);
return 0;
}