fork download
  1. #include <iostream>
  2. #include<vector>
  3. #include<unordered_map>
  4. using namespace std;
  5.  
  6. int main() {
  7. vector<int> nums={1,2,3};
  8. int k=2;
  9. int low=0;
  10. int high=0;
  11. int count=0;
  12. unordered_map<int,int> m;
  13.  
  14. for(high=0;high<nums.size();high++) {
  15. m[nums[high]]++;
  16. while(m.size()>k && low<=high) {
  17. m[nums[low]]--;
  18. if(m[nums[low]]==0) {
  19. m.erase(nums[low]);
  20. }
  21. low++;
  22. }
  23. count+=high-low+1;
  24. }
  25. cout<<count<<endl;
  26. return 0;
  27. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
5