-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
41 lines (31 loc) · 867 Bytes
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//
// Sorting(NumberOfDiscIntersections)
// https://app.codility.com/programmers/lessons/6-sorting/number_of_disc_intersections/
// Created by hyungwook on 2021/10/29.
//
import java.util.Arrays;
public class Solution {
public int solution(int[] A) {
int answer = 0;
int length = A.length;
long[] lt = new long[length];
long[] rt = new long[length];
for (int i = 0; i < length; i++) {
lt[i] = i - (long) A[i];
rt[i] = i + (long) A[i];
}
Arrays.sort(lt);
Arrays.sort(rt);
int pc = 0;
for (int i = 0; i < length; i++) {
while (pc < length && rt[i] >= lt[pc]) {
pc++;
}
answer += pc - 1 - i;
}
if (answer > 10000000) {
answer = -1;
}
return answer;
}
}