Advertisement
Singasking

Untitled

Jul 2nd, 2023
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. int Solution::solve(vector<int> &A, int B) {
  2. int start=0;
  3. int end=0;
  4.  
  5. int ans=0;
  6. int dCount=0;
  7. map<int,int> map;
  8. while(end<A.size()) {
  9.  
  10. if(dCount<=B) {
  11. if(map.find(A[end])==map.end())
  12. {
  13. map[A[end]]+=1;
  14. dCount+=1;
  15. } else if(map.find(A[end])!=map.end() && map.find(A[end])->second!=0) {
  16. //Value exists.
  17. map[A[end]]+=1;
  18.  
  19. }
  20. end+=1;
  21. } else if(dCount>B) {
  22. map[A[start]]-=1;
  23. if(map[A[start]]==0) dCount-=1;
  24. start+=1;
  25. }
  26.  
  27. if(dCount==B) {
  28. ans+=1;
  29. }
  30. }
  31. //if(dCount==B) { ans+=1; }
  32.  
  33.  
  34.  
  35.  
  36.  
  37. return ans;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement