Last active
August 29, 2015 14:19
-
-
Save gtomitsuka/568678ba9ac625526b0c to your computer and use it in GitHub Desktop.
Google Code Jam 2008 - Minimum Scalar Product - JavaScript
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
/* Copyright (c) 2015 by Gabriel Tomitsuka. | |
* Node.js code for GCJ's https://code.google.com/codejam/contest/32016/dashboard#s=p0 | |
* Minimum Scalar Product | |
*/ | |
var readline = require('readline'); | |
var async = require('async'); | |
var rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout | |
}); | |
rl.question('', function(testCases){ | |
var array = []; | |
array[Number(testCases))] = " "; | |
async.eachSeries(array, function(element, callback){ | |
rl.question('', function(howMany){ //Thanks JavaScript, result can be ignored :) | |
rl.question('', function(v1String){ | |
var v1Array = v1String.split(' '); | |
rl.question('', function(v2String){ | |
var v2Array = v2String.split(' '); | |
console.log(minimumScalarProduct(v1Array, v2Array)); | |
callback(); | |
}); | |
}); | |
}); | |
}) | |
}); | |
var orderArray = function(a, b){ //Ascending | |
return a - b; | |
} | |
var minimumScalarProduct = function(v1, v2){ | |
v1.sort(orderArray); | |
v2.sort(orderArray); | |
v2.reverse(); | |
var final = 0; | |
for(var location = 0; location < v1.length; location++){ | |
var m1 = Number(v1[location]); | |
var m2 = Number(v2[location]); | |
final += m1 * m2; | |
} | |
return final; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment