nabilgod
BAN USERJavaScript solution O(n).
function reorderArray(array, newOrder) {
var i;
var itemToMove;
var itemToMoveIndex;
for (i = 0; i < newOrder.length; i++) {
while(newOrder[i] !== i) {
itemToMove = array[i];
itemToMoveIndex = newOrder[i];
array[i] = array[itemToMoveIndex];
newOrder[i] = newOrder[itemToMoveIndex];
array[itemToMoveIndex] = itemToMove;
newOrder[itemToMoveIndex] = itemToMoveIndex;
}
}
return array;
}
JavaScript solution:
function getMinimumChanges(start, end, dictionary) {
var matchingChars;
var charsThatMatchEnd;
var newWordindex;
var newWordDistance;
var currentWord = start;
var count = 0;
dictionary = dictionary.filter(function(val, index, array) {
return getNumberOfMatchingChars(val, start) > 0 && getNumberOfMatchingChars(val, end) > 0;
});
// Add the end word to the dictionary
dictionary.push(end);
while (dictionary.length > 0 && currentWord !== end) {
count ++;
newWordCharsThatMatchEnd = 0;
newWordindex = -1;
dictionary.map(function (val, index) {
matchingChars = getNumberOfMatchingChars(val, currentWord);
if (matchingChars !== (start.length - 1)) {
// More than 1 char away so we can't use this
return;
}
// get the item closest to the end word from the dictionary
charsThatMatchEnd = getNumberOfMatchingChars(val, end);
if (charsThatMatchEnd > newWordCharsThatMatchEnd) {
newWordCharsThatMatchEnd = charsThatMatchEnd;
newWordindex = index;
}
});
if (newWordindex === -1) {
// There is no way to get to the end
return null;
}
currentWord = dictionary[newWordindex];
// remove the word from the dictionary so we don't get into a loop
dictionary.splice(newWordindex, 1);
}
function getNumberOfMatchingChars (first, second) {
var i;
var matchingIndex;
var matchingChars = 0;
for (i = 0; i < first.length; i++) {
matchingIndex = second.indexOf(first[i]);
if (matchingIndex === -1) {
continue;
}
matchingChars += 1;
}
return matchingChars;
}
return count;
}
My Javascript solution:
function getConsecutivesThatSum(numbers, target) {
var i;
var j;
var sum;
for (i = 0; i < numbers.length; i++) {
sum = numbers[i];
for (j = i+1; j < numbers.length; j++) {
sum += numbers[j];
if (sum > target) {
break;
} else if (sum < target) {
continue;
} else {
return { success: true, result: numbers.slice(i, j + 1)};
}
}
}
return { success: false };
}
getConsecutivesThatSum([1,3,5,7,9], 8);
// { success: true, result: [3,5] }
getConsecutivesThatSum([1,2,3,4,5], 7);
// { success: true, result: [3,4]}
getConsecutivesThatSum([1,3,5,7,9], 15);
// { success: true, result: [3,5,7]}
getConsecutivesThatSum([1,3,5,7,9], 6);
// { success: false}
getConsecutivesThatSum([6], 6);
// { success: false}
JavaScript Solution. I'm not exactly sure what the time complexity of this is.
I think in the best case it is O(n), and in the worst case it is O(n log n).
function minSubstring(source, elements) {
var sets = [];
var chars = {};
var history = {};
elements.split('').map(function(value) {
chars[value] = undefined;
});
source.split('').map(function(value, index) {
if (chars.hasOwnProperty(value)) {
history[value] = index;
if (Object.keys(history).length === elements.length) {
var range = Object.values(history);
range.sort(function(a,b){
return a-b
});
sets.push({
start: range[0],
end: range[range.length -1]
});
}
}
});
var range = Object.values(sets)
range.sort(function(a,b){
return (b.end - b.start) -(a.end - a.start);
});
var smallestRange = range.pop();
return source.slice(smallestRange.start, smallestRange.end +1);
}
I have done a JavaScript solution that is O(n log n) due to the sort.
function getNonIntersetingArrayMerge(first, second) {
var workingItem;
var items = first.concat(second);
var results = [];
items.map(function (val, index, array) {
array[index] = val.split('-').map(Number);
});
items.sort(function (a, b) {
return a[0] - b[0];
});
workingItem = items.splice(0,1)[0];
while(items.length > 0) {
item = items.splice(0,1)[0];
if (item[0] < workingItem[1]) {
workingItem[1] = item[1];
continue;
}
results.push(workingItem);
workingItem = item;
}
results.push(workingItem);
results.map(function (val, index, array) {
array[index] = val.join('-');
});
return results;
}
var first = ['3-11', '17-25', '58-73'];
var second = ['6-18', '40-47'];
getNonIntersetingArrayMerge(first, second);
I took a different approach, I converted the array to a number, added the two numbers, then just added any extra leading 0's.
function addNumberToArray(number, array) {
var i;
var arrayNumber = parseInt(array.join(''), 10);
var newNumberArray = (arrayNumber + number).toString().split('');
var difference = array.length - newNumberArray.length;
if (difference > 0) {
for (i = 0; i < difference; i++) {
newNumberArray.unshift(0);
}
}
return newNumberArray;
}
Javascript Solution
- nabilgod January 16, 2017