본문 바로가기

코딩테스트/codility

PermCheck

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

 

1~N까지 중복된 숫자가 없는 순열인지 체크하는 문제

 

주어진 배열을 정렬하고 인덱스값과 비교하는 방법이 먼저 떠올랐는데

 

굳이 정렬하지 않고 중복 체크 + 범위 체크정도만 해도 풀릴것 같았다.

 

class Solution {
    public int solution(int[] A) {
        boolean[] check = new boolean[A.length+1];

        for (int a : A) {
            if (a > A.length) return 0;
            if (check[a]) return 0;
            check[a] = true;
        }

        return 1;
    }
}

 

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

Distinct  (0) 2023.09.29
FrogRiverOne  (0) 2023.09.29
TapeEquilibrium  (0) 2023.09.18
OddOccurrencesInArray  (0) 2023.09.16
CyclicRotation  (0) 2023.09.11