Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int Solution::solve(vector<int> &A, int B) {
- int start=0;
- int end=0;
- int ans=0;
- int dCount=0;
- map<int,int> map;
- while(end<A.size()) {
- if(dCount<=B) {
- if(map.find(A[end])==map.end())
- {
- map[A[end]]+=1;
- dCount+=1;
- } else if(map.find(A[end])!=map.end() && map.find(A[end])->second!=0) {
- //Value exists.
- map[A[end]]+=1;
- }
- end+=1;
- } else if(dCount>B) {
- map[A[start]]-=1;
- if(map[A[start]]==0) dCount-=1;
- start+=1;
- }
- if(dCount==B) {
- ans+=1;
- }
- }
- //if(dCount==B) { ans+=1; }
- return ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement