关于Java:请帮助我了解此Codility测试

Please help me to understand this codility test

给出以下的Codility测试:

Given an array A of N integers, we draw N discs in a 2D plane such that the I-th
disc is centered on (0,I) and has a radius of A[I]. We say that the J-th disc and
K-th disc intersect ifJ a‰ KandJ-th andK`-th discs have at least one common
point.

Write a function class Solution { public int solution(int[] A); } that, given an array
A describing N discs as explained above, returns the number of pairs of intersecting
discs. For example, given N=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:

-Nis 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个元素,但是可能的对数为6*5/2=15(一般形式:n(n-1)/2)),因此,即使有6个点,也可能有多达15个(包括)交点,如上所述。

磁盘数量不是最大15,因为某些"磁盘"不相交,例如,磁盘(0,0)和磁盘(0,5)没有公共点。 (0,0)包含点{(0,0), (0,1)},而(0,5)磁盘包含点{ (0,5) }。由于这两个集合的交集为空-(0,0);(0,5)不是有效的磁盘对,因此不应包含。铅>


这11对与问题中列出的完全相同。每个光盘都以(0, I)居中,因此每个光盘与两个相邻光盘的距离均为1个距离单位(光盘0和光盘N-1除外,它们只有一个相邻光盘)。给定特定数组A:

  • 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,因此没有光盘而相交
如果仅从该列表中计算唯一对(例如2个相交3和3个相交2与2,它们合计为1),则得出11个相交。