[C++][Distinct Numbers in Window] sliding window O(n)


#1
vector<int> Solution::dNums(vector<int> &A, int B) {
    int len = A.size();
    vector< int > vec;
    unordered_map< int, int > umap;
    if( B > len )                           return vec;
    for ( int i = 0; i < B; i ++ )          umap[A[i]] ++;
    vec.push_back( umap.size() );
    int count = vec[0];
    int prevPos = 0, nextPos = B;
    while(nextPos < len) {
        if( !( -- umap[A[prevPos]] ))       count --;
        if( ++ umap[A[nextPos]] == 1)       count ++;
        vec.push_back( count );
        nextPos ++, prevPos ++;
    }
    return vec;
}