Skip to content

Instantly share code, notes, and snippets.

@barreyro
Last active September 2, 2016 21:31
Show Gist options
  • Save barreyro/45abc5a34a01657b8b44df279e372220 to your computer and use it in GitHub Desktop.
Save barreyro/45abc5a34a01657b8b44df279e372220 to your computer and use it in GitHub Desktop.
Algorithm Find Intersection
//Problem:
// -There are X columns
// -There are Y rows
// -People in different areas want to meet in 1 location
// -Input: array of points
// -Find an intersection which minimizes the total walking distance of all people
// -Data structure for output is an array
function getMedian(numberArr){
var length = numberArr.length;
if (length % 2 === 0){
var firstMedian = numberArr / 2;
var secondMedian = firstMedian - 1;
return ((numberArr[firstMedian] + numberArr[secondMedian])/ 2);
} else {
var medianPosition = Math.floor(length / 2);
return numberArr[medianPosition];
};
}
function getIntersection(pointsArr){
var rowArr = [];
var columnArr = [];
for (var count = 0; count < pointsArr.length; count++){
var rowValue = pointsArr[count][0];
var columnValue = pointsArr[count][1];
rowArr.push(rowValue);
columnArr.push(columnValue);
}
var rowMedian = getMedian(rowArr);
var colMedian = getMedian(columnArr);
return [rowMedian, colMedian];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment