-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberOfDiscIntersections.js
More file actions
44 lines (33 loc) · 970 Bytes
/
NumberOfDiscIntersections.js
File metadata and controls
44 lines (33 loc) · 970 Bytes
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
//Score: 100%
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
var topLimit = 10000000;
var ret = 0;
var sides = new Array(A.length);
for(var i=0; i < A.length; i++){
sides[i] = [i-A[i], i+A[i]];
}
sides.sort((a,b) => a[0] - b[0]);
for(var i=0; i < A.length; i++){
var cur = sides[i]
for(var j=i+1; j < A.length; j++){
var comp = sides[j];
if(comp[0] <= cur[1]){
ret++;
} else {
break;
}
}
if(ret > topLimit){
ret = -1;
break;
}
}
return ret;
}