Skip to content

Instantly share code, notes, and snippets.

@andrewhl
Last active Mar 25, 2017
Embed
What would you like to do?
function orderNum(arr, num) {
let i = arr.length - 2;
let temp;
if (arr[i] < num) {
return arr;
}
while (arr[i] > num) {
temp = arr[i];
arr[i] = num;
arr[i + 1] = temp;
// if (arr[i] === num) {
// arr.splice(arr[i], 1);
// break;
// }
i -= 1;
}
return arr;
}
function dblLinear(n) {
let result = [1];
for (let i = 0; i <= n; i++) {
const x = result[i];
const y = 2 * x + 1;
const z = 3 * x + 1;
if (result[result.length - 1] !== y) {
result.push(y);
result = orderNum(result, y);
}
if (result[result.length - 1] !== z) {
result.push(z);
result = orderNum(result, z);
}
}
result = result.filter((num, pos) => result.indexOf(num) === pos);
return result[n];
}
function orderNum(arr, num) {
let i = arr.length - 1;
let temp;
arr.push(num);
while (arr[i] > num) {
temp = arr[i];
arr[i] = num;
arr[i + 1] = temp;
if (arr[i-1] === num) {
arr.splice([i-1], 1);
break;
}
i -= 1;
}
return arr;
}
function dblLinear(n) {
let result = [1];
for (let i = 0; i <= n; i++) {
const x = result[i];
const y = 2 * x + 1;
const z = 3 * x + 1;
result = orderNum(result, y);
result = orderNum(result, z);
}
return result[n];
}
function dblLinear(n) {
let result = [1];
for (let i = 0; i <= n; i++) {
const x = result[i];
const y = 2 * x + 1;
const z = 3 * x + 1;
if (!result.includes(y)) {
const largerYIndex = result.findIndex((num) => num > y);
(largerYIndex !== -1) ? result.splice(largerYIndex, 0, y) : result.push(y);
}
if (!result.includes(z)) {
const largerZIndex = result.findIndex((num) => num > z);
(largerZIndex !== -1) ? result.splice(largerZIndex, 0, z) : result.push(z);
}
}
result = result.sort((a,b) => a - b);
return result[n];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment