[algorithm] MissingInteger, PermCheck, CountDiv

yeon·2021년 4월 7일
0

Missing Integer

MissingInteger coding task - Learn to Code - Codility

배열 A에 포함되지 않은 가장 작은 숫자 리턴하기

public static int solution(int[] A) {
    Arrays.sort(A);
    int min = 1;
    for(int i = 0; i < A.length; i++){
        if(A[i] <= 0) {
            continue;
        }

        if(A[i] == min) {
            min++;
        }
    }
    return min;
}

PermCheck

PermCheck coding task - Learn to Code - Codility

1부터 N까지 배열에 모든 숫자가 하나씩 있으면 1 return, 아니면 0 return

public int solution(int[] A) {
    Arrays.sort(A);
    for (int i = 0; i < A.length; i++) {
        if (i + 1 == A[i]) {
            continue;
        } else {
            return 0;
        }
    }
    return 1;
}

CountDiv

CountDiv coding task - Learn to Code - Codility

A부터 B까지 숫자 중에 K로 나누어 떨어지는 숫자의 개수 구하기

public int solution(int A, int B, int K) {
    int count1 = A / K;
    int count2 = B / K;
    if ((A % K == 0 && B % K == 0) || (A % K == 0 && B % K != 0)) {
        return count2 - count1 + 1;
    }
    return count2 - count1;
}

2개의 댓글

comment-user-thumbnail
2021년 4월 9일

알고리즘 화이팅입니다!

1개의 답글