-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathNumberOfDiscIntersections.cs
49 lines (46 loc) · 1020 Bytes
/
NumberOfDiscIntersections.cs
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
42
43
44
45
46
47
48
49
using System;
using System.Collections;
class Solution {
public class Comparer : IComparer {
public int Compare(object x, object y) {
Circle a = (Circle)x, b = (Circle)y;
return a.left.CompareTo(b.left) == 0 ? a.position.CompareTo(b.position) : a.left.CompareTo(b.left);
}
}
class Circle {
public long radius;
public long position;
public long left {
get {
return position - radius;
}
}
public long right {
get {
return position + radius;
}
}
}
public int solution(int[] A) {
int count = 0;
var circles = new Circle[A.Length];
for (var i = -1; ++i < A.Length;) {
circles[i] = new Circle {
radius = A[i],
position = i
};
}
var list = new List<Circle>();
Array.Sort(circles, new Comparer());
for (var i = -1; ++i < circles.Length;) {
for (var j = list.Count; --j >= 0;) {
if (list[j].right < circles[i].left)
list.RemoveAt(j);
}
if ((count += list.Count) > 1e7)
return -1;
list.Add(circles[i]);
}
return count;
}
}