Created
August 8, 2019 15:36
-
-
Save crazy4groovy/aacd9b8f39bdc4cf404cb8b39c3352ac to your computer and use it in GitHub Desktop.
How many straight lines, starting from point {x: 0, y: 0}, are needed to intersect with all the points in a given array?
This file contains 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 normalize({x, y}) { | |
const nx = x / Math.abs(x) | |
const ny = y / Math.abs(x) | |
return nx + '|' + ny | |
} | |
/* | |
console.log(normalize({x: 1, y: 2})) | |
console.log(normalize({x: 2, y: 4})) | |
console.log(normalize({x: -2, y: 4})) | |
console.log(normalize({x: 2, y: -4})) | |
console.log(normalize({x: -2, y: -4})) | |
console.log(normalize({x: -1, y: -2})) | |
console.log(normalize({x: -1, y: -4})) | |
*/ | |
function solution(A) { | |
const nA = A.map(i => normalize(i)) | |
const uniqueA = [...new Set(nA)] | |
return uniqueA.length | |
} | |
/* | |
console.log(solution([{x:1, y:1}, {x:2, y:2}])) // 1 | |
console.log(solution([{x:-1, y:1}, {x:-2, y:2}])) // 1 | |
console.log(solution([{x:-1, y:-1}, {x:-2, y:-2}])) // 1 | |
console.log(solution([{x:-1, y:-1}, {x:2, y:2}])) // 2 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment