Created
September 5, 2018 03:14
-
-
Save lastday154/50c5d15d1ad490df80f937f63594797d to your computer and use it in GitHub Desktop.
min distance
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 compare(a,b) { | |
if (a.distance < b.distance) { | |
return -1; | |
} | |
if (a.distance > b.distance) { | |
return 1; | |
} | |
return 0; | |
} | |
function ClosestXdestinations(numDestinations, allLocations, numDeliveries) | |
{ | |
// WRITE YOUR CODE HERE | |
let distances = []; | |
allLocations.forEach((location, index) => { | |
const distance = Math.sqrt(Math.pow(location[0], 2) + Math.pow(location[1], 2)); | |
distances.push({index, distance}); | |
}) | |
distances = distances.sort(compare); | |
const indexLocations = distances.slice(0, numDeliveries).map((item) => { | |
return item.index; | |
}); | |
const result = []; | |
for(let i=0; i< numDeliveries; i++) { | |
result.push(allLocations[indexLocations[i]]); | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment