O(n) solution C++ using maps


#1

vector Solution::dNums(vector &A, int B) {

vector<int> v;
int i,prev;
map<int,int> m;

for(i=0;i<B;i++)
    m[A[i]]++;

prev=0;
v.push_back(m.size());

for(;i<A.size();i++){
    m[A[prev]]--;
    
    if(m[A[prev]]==0)
        m.erase(A[prev]);

    m[A[i]]++;
    prev++;
    v.push_back(m.size());
}

return v;

}