sliver__

Codility : Lesson 4 (Counting Elem) - PermCheck 본문

CS/Codility

Codility : Lesson 4 (Counting Elem) - PermCheck

sliver__ 2021. 9. 28. 23:53
728x90

안녕하세요~~

디벨롭퍼입니다~~~

https://app.codility.com/programmers/lessons/4-counting_elements/perm_check/

 

PermCheck coding task - Learn to Code - Codility

Check whether array A is a permutation.

app.codility.com

***********************************************************************

풀어보실 분들을 위해서 링크 걸어두었습니다.

***********************************************************************

 

주어진 문제는 아래와 같습니다.

 

PermCheck

N개의 숫자가 담겨있는 A배열에서 

배열이 연속적인 숫자를

모두 포함하고 있는지 찾는 문제입니다.

 

주어진 벡터를 전부 순회해서 가장 큰 값을 찾고 

값들을 set에 넣었을 때, 

가장 큰 값만큼 set에 들어있어야 하니 

비교를 하면 되겠다고 생각이 들더군요.

 

아래는 제출한 코드입니다.

// you can use includes, for example:
// #include <algorithm>
#include <set>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;

int solution(vector<int> &A) {
    // write your code in C++14 (g++ 6.2.0)
    int ret = 0;
    int max = -1;
    bool duplicate = false;
    std::set<int> s;
    for(std::vector<int>::iterator iter=A.begin(); iter!=A.end(); ++iter)
    {
        if(max < *iter) max = *iter;
        if(s.find(*iter)  != s.end())
        {
            duplicate =  true;
            break;
        }
        else
        {
            s.insert(*iter);
        }
    }
    if(duplicate) ret = 0;
    else if((int)s.size() == max) ret=1;
    return  ret;
}

이 문제에서 예외처리 해야할 점이 하나있는데요,

중복된 숫자가 있을 경우 permutation이 아닌조건 입니다.

이 경우를 따로 처리를 해줘야 하더라구요.

 

1일 1문제

역시 꾸준히는 정말 어려운 것 같아요 ㅠㅠ

그럼 이만~~

728x90
Comments