Takhti
BAN USER/*assuming both producers and consumres are sorted by x. The premise is that in order to find the closest producer to consumer i, we only need to look at producer of consumer i-1 onwards on x axis*/
function findNearestProducer(producers, consumer, startIndex) {
var closestProducerIndex = startIndex,
producer,
distance,
closestDistance;
for(; startIndex < producers.length; startIndex++) {
producer = producers[startIndex];
distance = Math.pow(producer.x - consumer, 2) + Math.pow(producer.y,2);
if(closestDistance === undefined || distance < closestDistance) {
closestProducerIndex = startIndex;
closestDistance = distance;
}
}
return closestProducerIndex;
}
function printNearestProducers(producers, consumers) {
var i, j, consumer, producer, lastProducerIndex = 0;
for(i = 0; i < consumers.length; i++) {
consumer = consumers[i];
lastProducerIndex = findNearestProducer(producers, consumer, lastProducerIndex);
console.log(JSON.stringify(consumer) + " " + JSON.stringify(producers[lastProducerIndex]) + "\n");
}
}
printNearestProducers([{x: 0, y: 3},{x: 1, y: 1},{x: 3, y: 2},{x: 8, y: 10},{x: 9, y: 100}], [1, 5, 7]);
function addToTrie(trie, entry) {
if(!entry) return;
var i, node = trie, parent;
for(i =0; i < entry.length && node; i++) {
parent = node;
node = parent[entry[i]];
}
if(i === entry.length || !parent) {
return;
}
i--;
for(; i < entry.length; i++) {
parent = parent[entry[i]] = {};
}
parent.isLeaf = true;
}
function getTrieCount(trie, entry, index, isExactMatch) {
var value, node = trie;
if(index >= entry.length && trie) {
return (isExactMatch ? 0 : 1);
}
if(!trie) {
return 0;
}
index = index || 0;
value = entry[index];
// if value is zero, find in all sub-tries, otherwise exact match is required
if(value !== '0') {
if(trie[value]) {
return getTrieCount(trie[value], entry, index + 1, isExactMatch);
} else {
return false;
}
}
// look into all sub children;
var key, count = 0;
for(key in trie) {
count += getTrieCount(trie[key], entry, index + 1, (isExactMatch && key === '0'));
}
return count;
}
function buildTrie(entryList) {
var trie = {};
var i, entry;
for( i = 0; i < entryList.length; i++) {
entry = entryList[i];
addToTrie(trie, entry);
}
var count =0;
for( i = 0; i < entryList.length; i++) {
entry = entryList[i];
count += getTrieCount(trie, entry, 0, true);
}
return count;
}
console.log(buildTrie(['0b111', '0b101', '0b010']));
function sumWindow(array, target) {
var startIndex = 0;
var endIndex = 1;
var total = array[startIndex] + array[endIndex];
while(endIndex < array.length && startIndex < array.length -1) {
if(total === target) {
break;
}
if(total < target || (endIndex - startIndex === 1)) {
endIndex++;
total += array[endIndex];
} else {
total -= array[startIndex];
startIndex++;
}
}
return (total === target);
}
console.log(sumWindow([1, 3, 5, 18], 40));
RepWilliamDGiles, Cloud Support Associate at ADP
Spent 2001-2006 creating marketing channels for tar worldwide. Was quite successful at building tobacco for farmers. Won several awards for ...
RepSoccer lover, coffee addict, guitarist, International Swiss style practitioner and TDC honorary member. Acting at the nexus of simplicity and ...
Rep
The code below implements booking and querying for availability, release is not implemented but you can get the idea from booking... (It is in JavaScript and you can test it in coderpad.io)
- Takhti October 02, 2016