Skip to content

Instantly share code, notes, and snippets.

@lastday154
Last active September 7, 2018 02:26
Show Gist options
  • Save lastday154/9ee6e4c80004ff68681822fe84b933ef to your computer and use it in GitHub Desktop.
Save lastday154/9ee6e4c80004ff68681822fe84b933ef to your computer and use it in GitHub Desktop.
optimalUtilization
function optimalUtilization(maximumOperatingTravelDistance,
forwardShippingRouteList, returnShippingRouteList)
{
// WRITE YOUR CODE HERE
let currentMax = 0;
const result = [];
forwardShippingRouteList.forEach((forwardDistance) => {
returnShippingRouteList.forEach((returnDistance) => {
let distance = forwardDistance[1] + returnDistance[1];
if (currentMax <= distance && distance <= maximumOperatingTravelDistance) {
if (result.length > 0 && currentMax < distance) {
result = [];
}
currentMax = distance;
result.push([forwardDistance[0], returnDistance[0]]);
}
});
});
return result;
}
console.log(optimalUtilization(20, [[1,8], [2,15], [3,9]], [[1,8], [2,11], [3,12]]));
@sudheesh001
Copy link

function optimalUtilization(maximumOperatingTravelDistance, forwardShippingRouteList, returnShippingRouteList) {
	map1 = {};
	map2 = {};
	forwardShippingRouteList.forEach((pair) => { map1[pair[0]] = pair[1] });
	returnShippingRouteList.forEach((pair) => { map2[pair[0]] = pair[1] });
	array1 = Object.keys(map1);
	array2 = Object.keys(map2);
    possibilities = array1.reduce((res, one) => res.concat(array2.map(two => [[one, two]])), []);

    results = {};
    possibilities.forEach((possibility) => {
    	start = possibility[0][0];
    	retur = possibility[0][1];
    	d1 = map1[start];
    	d2 = map2[retur];
    	s = d1+d2;
    	if (s <= maximumOperatingTravelDistance) {
    		// Do not ever consider non optimal cases
    		if (s in results) {
	    		r = results[s];
	    		r.push([start, retur])
	    		results[s] = r;
	    	}
	    	else {
	    		results[s] = [[start, retur]]
	    	}
    	}
    })

    if (maximumOperatingTravelDistance in results) {
    	return results[maximumOperatingTravelDistance];
    }
    else {
    	ds = Object.keys(results).sort().reverse()
    	return results[ds[0]];
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment