Created
January 25, 2018 23:09
-
-
Save bogdanpetru/912ff176945b4c820bf8b83a249b1453 to your computer and use it in GitHub Desktop.
Training ticket - Solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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