Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@andrewhl
Last active March 25, 2017 23:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewhl/eac98cf083a60d90067bab84c9922f79 to your computer and use it in GitHub Desktop.
Save andrewhl/eac98cf083a60d90067bab84c9922f79 to your computer and use it in GitHub Desktop.
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