[codility]Prefix-set

// you can also use includes, for example:
// #include <algorithm>
int solution(const vector<int> &A) {
    // write your code in C++98
    //...keep record of all the elements that exist in the array
    vector<bool> existNum(A.size(), false);
    int distinctNumCnt = 0;
    for(int i = 0; i < A.size(); ++i)
    {
        if(!existNum[A[i]]) distinctNumCnt++;
        existNum[A[i]] = true;
    }
    //...then, check if all the elements show up so far
    int curDistinctNumCnt = 0;
    for(int i = 0; i < A.size(); ++i)
    {
        if(existNum[A[i]])
        {
            existNum[A[i]] = false;
            curDistinctNumCnt++;
        }
        if(curDistinctNumCnt == distinctNumCnt) return i;
    }
    
}

你可能感兴趣的:([codility]Prefix-set)