Simple CPP Solution in O(n)


#1

vector Solution::dNums(vector &A, int B)
{
int n = A.size(),sz,k = B;
vector res;
if(n == 0)
return res;
map <int,int> mp;
for(int i = 0; i < k; ++i)
{
mp[A[i]]++;
}
sz = mp.size();
res.push_back(sz);
for(int i = 1; i < n-k+1; ++i)
{
mp[A[i-1]] -= 1;
if(mp[A[i-1]] == 0)
mp.erase(A[i-1]);
mp[A[i+k-1]]++;
sz = mp.size();
res.push_back(sz);
}
return res;
}