Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,33 @@
function getLis(input) {
if (input.length === 0) {
return [];
}
var lisLenPerIndex = [];
let max = { index: 0, length: 1 };
for (var i = 0; i < input.length; i++) {
lisLenPerIndex[i] = 1;
for (var j = i - 1; j >= 0; j--) {
if (input[i] > input[j] && lisLenPerIndex[j] >= lisLenPerIndex[i]) {
var length = lisLenPerIndex[i] = lisLenPerIndex[j] + 1;
if (length > max.length) {
max = { index: i, length };
}
}
}
}
var lis = [input[max.index]];
for (var i = max.index; i >= 0 && max.length !== 0; i--) {
if (input[max.index] > input[i] && lisLenPerIndex[i] === max.length - 1) {
lis.unshift(input[i]);
max.length--;
}
}
return lis;
}
console.log(getLongestIncreasingSubsequence([0, 7, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15]));
console.log(getLongestIncreasingSubsequence([3, 2, 6, 4, 5, 1]));

View file

@ -0,0 +1,39 @@
function getLIS(input) {
if (input.length === 0) {
return 0;
}
const piles = [input[0]];
for (let i = 1; i < input.length; i++) {
const leftPileIdx = binarySearch(piles, input[i]);
if (leftPileIdx !== -1) {
piles[leftPileIdx] = input[i];
} else {
piles.push(input[i]);
}
}
return piles.length;
}
function binarySearch(arr, target) {
let lo = 0;
let hi = arr.length - 1;
while (lo <= hi) {
const mid = lo + Math.floor((hi - lo) / 2);
if (arr[mid] >= target) {
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return lo < arr.length ? lo : -1;
}
console.log(getLongestIncreasingSubsequence([0, 7, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15]));
console.log(getLongestIncreasingSubsequence([3, 2, 6, 4, 5, 1]));