O(n) map Solution


#1
vector<int> Solution::dNums(vector<int> &A, int B) {
    vector<int> res;
    if(B > A.size()) return res;
    int i = 0;
    unordered_map<int,int> m;
    int cnt = 0;
    while(i < B) 
    {
        cnt++;
        if(m[A[i++]]++ >= 1) cnt--;
    }
    // cout<<cnt<<endl;
    res.push_back(cnt);
    for(int j = 0; i < A.size(); j++,i++) {
        m[A[j]]--;
        if(m[A[j]] == 0) cnt--;
        m[A[i]]++;
        if(m[A[i]] == 1) cnt++;
        res.push_back(cnt);
    }   
    return res;
}