[Codility] FirstUnique

 

https://app.codility.com/programmers/trainings/4/first_unique/start/

 

Codility

Your browser is not supported Please, update your browser or switch to a different one. Learn more about what browsers are supported

app.codility.com

 


 

해결전략

 

정렬 sort


 

정답 코드

 

#include <queue>
#include <map>
#include <algorithm>

int solution(vector<int> &A) {
    
    int n = A.size();
    queue<int> Q;
    map<int, int> myMap;

    for(int i = 0; i < n; i++){
        Q.push(A[i]);
        if(myMap.find(A[i]) != myMap.end()){
            myMap[A[i]]++;
        }
        else{
            myMap[A[i]] = 1;
        }
    }

    int answer = -1;
    while(!Q.empty())
    {
        int front = Q.front();
        Q.pop();

        if(myMap[front] == 1){
            answer = front;
            break;
        }
    }

    return answer;
}

 


'⭐ 코딩테스트 > Codility' 카테고리의 다른 글

[Codility] Array Inversion Count  (0) 2024.02.18
[Codility] Tree Height  (0) 2024.02.18
[Codility] Tree Longest Zigzag  (0) 2024.02.18
[Codility] The Matrix  (0) 2024.02.17
[Codility] MaxNonoverlappingSegments  (1) 2024.02.06