Please help me to understand this codility test
给出以下的Codility测试:
Given an array
A ofN integers, we drawN discs in a 2D plane such that theI -th
disc is centered on(0,I) and has a radius ofA[I] . We say that theJ -th disc and
K-th disc intersect if J a‰ Kand J-th and K`-th discs have at least one common
point.Write a function
class Solution { public int solution(int[] A); } that, given an array
A describingN discs as explained above, returns the number of pairs of intersecting
discs. For example, givenN=6 and:
A[0] = 1 A[1] = 5 A[2] = 2
A[3] = 1 A[4] = 4 A[5] = 0 Intersecting discs appear in eleven pairs of elements:
0 and 1,
0 and 2,
0 and 4,
1 and 2,
1 and 3,
1 and 4,
1 and 5,
2 and 3,
2 and 4,
3 and 4,
4 and 5.so the function should return
11 .The function should return
a?’1 if the number of intersecting pairs exceeds 10,000,000.
Assume that:-
N is an integer within the range[0..100,000] ;
- Each element of array A is an integer within the range [0..2147483647].Complexity
- Expected worst-case time complexity is O(N*log(N));
- Expected worst-case space complexity is O(N), beyond input storage (not counting the
storage required for input arguments).
这11对是哪里来的,因为只有6个元素?
只有6个元素,但是可能的对数为
磁盘数量不是最大15,因为某些"磁盘"不相交,例如,磁盘
这11对与问题中列出的完全相同。每个光盘都以
- disc 0的半径为1,因此它与光盘1相交。
- disc 1的半径为5,因此与光盘0、2、3相交。 ,4,5
- 光盘2的半径为2,因此它与光盘0、1、3、4相交。
- 光盘3的半径为1,因此与光盘2、4
- 光盘4的半径为4,因此它与光盘0、1、2、3、5相交
- 光盘5的半径为0,因此没有光盘而相交