[Codility] PermCheck

hamsteak·2023년 10월 7일
0

ps

목록 보기
22/39

주어진 배열의 원소들이 순열을 이루는지 확인해야 하는 문제.

중복을 검사하고 방문 횟수와 배열 내 최댓값을 비교해서 순열을 이루는지 확인한다.

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

cpp code

bool visited[100001];
int visit_cnt = 0;
int highest = 0;
int solution(vector<int> &A) {
    for (int i : A) {
        if (i > 100000) return 0;
        if (visited[i]) return 0;
        visited[i] = true;
        visit_cnt++;
        highest = max(highest, i);
    }
    return visit_cnt == highest;
}
profile
안녕하세요

0개의 댓글