Skip to content

Instantly share code, notes, and snippets.

@bogdanpetru
Created January 25, 2018 23:09
Show Gist options
  • Save bogdanpetru/912ff176945b4c820bf8b83a249b1453 to your computer and use it in GitHub Desktop.
Save bogdanpetru/912ff176945b4c820bf8b83a249b1453 to your computer and use it in GitHub Desktop.
Training ticket - Solution
function intersects(A, a, b) {
const rangeAStart = a - A[a];
const rangeAEnd = a + A[a];
const rangeBStart = b - A[b];
const rangeBEnd = b + A[b];
return (
// the start of next range is included
rangeAStart <= rangeBStart && rangeAEnd >= rangeBStart
||
//
// front is included in second part
rangeAStart <= rangeBEnd && rangeAStart >= rangeBStart
);
}
function solution(A) {
let count = 0;
for (let i = 0, len = A.length; i < len; i++) {
for (let j = i + 1; j < len; j++) {
count = intersects(A, i, j) ? count + 1 : count;
if (count > 10000000) {
return -1;
}
}
}
return count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment