C++ soln O(n) without erasing map


#1

vector Solution::dNums(vector &A, int B) {
map<int,int> m;
vector sol;
for(int i=0;i<B;++i)
m[A[i]]++;
sol.push_back(m.size());
int j=B;
int i=0;
while(j<A.size()){
if(m[A[i]]==1)
m.erase(A[i]);
else
m[A[i]]–;
i++;
m[A[j]]++;
sol.push_back(m.size());
j++;
}
return sol;
}