intSolution::solve(vector<int> &A, int B){ int res = 0; unordered_map<int, int> freq; unordered_map<int, int> mi; int l = 0, r = 0, m = 0, n = A.size(); while(r < n) { while(r < n and freq.size() <= B) { if(freq.size() == B) { int m = l; unordered_map<int, int> now; while(m < r and freq[A[m]] - now[A[m]] > 1) { now[A[m]] += 1; m += 1; } res += m - l + 1; } freq[A[r]] += 1; r += 1; } while(l < r and freq.size() > B) { if(--freq[A[l]] == 0) freq.erase(A[l]); l += 1; } } if(freq.size() == B) { int m = l; unordered_map<int, int> now; while(m < r and freq[A[m]] - now[A[m]] > 1) { now[A[m]] += 1; m += 1; } res += m - l + 1; } return res; }