Advertisement
unknown_0711

Untitled

Nov 17th, 2022
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. int Solution::solve(vector<int> &A, int B)
  2. {
  3. int cnt=0, i=0, j=0, n=A.size();
  4. map<int,int> hash;
  5. hash[A[0]]++;
  6. while(j<A.size()){
  7. if(hash.size()<=B)cnt+=(j-i+1);
  8. if(hash.size()<=B){
  9. j++;
  10. if(j<n)hash[A[j]]++;
  11. }
  12. else{
  13. hash[A[i]]--;
  14. if(hash[A[i]]==0)hash.erase(A[i]);
  15. i++;
  16. }
  17. }
  18. hash.clear();
  19. i=0,j=0;
  20. int cnt2=0;
  21. hash[A[0]]++;
  22. while(j<A.size())
  23. {
  24. if(hash.size()<=B-1)cnt2+=(j-i+1);
  25. if(hash.size()<=B-1){
  26. j++;
  27. if(j<n)hash[A[j]]++;
  28. }
  29. else{
  30. if(i==j){
  31. j++;
  32. if(j<n)hash[A[j]]++;
  33. continue;
  34. }
  35. hash[A[i]]--;
  36. if(hash[A[i]]==0)hash.erase(A[i]);
  37. i++;
  38. }
  39. }
  40. B--;
  41. return cnt-cnt2;
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement