반응형
Recent Posts
Recent Comments
관리 메뉴

개발잡부

[codility] NumberOfDiscIntersections 본문

이직

[codility] NumberOfDiscIntersections

닉의네임 2022. 7. 25. 21:34
반응형

 

public static int solution(int[] A) {
    // write your code in Java SE 8

    int N = A.length;
    long[] lower = new long[N];
    long[] upper = new long[N];

    for (int i = 0; i < N; i++) {
        lower[i] = i - (long) A[i];
        upper[i] = i + (long) A[i];
    }

    Arrays.sort(lower);
    Arrays.sort(upper);

    int intersection = 0;
    int j = 0;

    for (int i = 0; i < N; i++) {
        while (j < N && upper[i] >= lower[j]) {
            intersection += j;
            intersection -= i;
            j++;
        }
    }

    if (intersection > 10000000) return -1;
    return intersection;

}
반응형

'이직' 카테고리의 다른 글

[codility] Nesting  (0) 2022.07.26
[codility] Brackets  (0) 2022.07.25
[codility] Triangle  (0) 2022.07.25
[codility] MaxProductOfThree  (0) 2022.07.25
[codility] Distinct  (0) 2022.07.24
Comments