-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberOfDiscIntersections.js
More file actions
39 lines (34 loc) · 1.36 KB
/
Copy pathNumberOfDiscIntersections.js
File metadata and controls
39 lines (34 loc) · 1.36 KB
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
const MAX = 10000000;
function solution(A) {
var disks = [];
var start, end;
var activeDisks = 0;
var intersections = 0;
/* Initial an arrays of disks with zero-ed start and end points. */
for (i = 0; i < A.length; i++) {
disks.push([0,0]);
}
/* Based on the start and end points for each disk, create an array of disks for each point.
* Avoid adding < 0 or > array length
*/
for (i = 0; i < A.length; i++) {
start = i > A[i] ? i - A[i] : 0;
end = i + A[i] < A.length - 1 ? i + A[i] : A.length - 1;
disks[start][0]++;
disks[end][1]++;
}
/* Loop through each point. Count the number of intersections based on the amount of active disks.
* Afterwards update the number of active disks based on the start and end points.
*/
for (j = 0; j < A.length; j++) {
intersections += activeDisks * disks[j][0]; // Add intersections from the new discs and currently active disks.
intersections += disks[j][0] > 0 ? disks[j][0] * (disks[j][0] - 1) / 2 : 0; // Add intersections between the newly added disks.
activeDisks += disks[j][0]; // Update added disks
activeDisks -= disks[j][1]; // Update removed disks
if (intersections > MAX) {
intersections = -1;
break;
}
}
return intersections;
}