Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Longest Increasing Subsequence in Javascript #988

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
function LongestIncreasingSubsequence(input) {

const longestArr = Array(input.length).fill(1);

let prev = 0;
let curr = 1;
let aux = [],result = [];
let ans = 0;
while (curr < input.length) {
while(prev<curr)
{
if (input[prev] < input[curr]) {
const l = longestArr[prev] + 1;
if (l > longestArr[curr]) {
longestArr[curr] = l;
aux.push(input[prev]);
}
}
prev += 1;
}
if(aux.length===0 || input[curr]>aux[aux.length-1])
{
aux.push(input[curr]);
}
if(longestArr[curr]>ans)
{
ans=longestArr[curr];
result = aux;
}
aux=[];
curr += 1;
prev = 0;
}
obj = {'array':input,'length':ans, 'subarray':result};
return obj;
}

var x = LongestIncreasingSubsequence([0, 7, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15]);
x = LongestIncreasingSubsequence([0,0,0,0,0,0,1,1,1,1,2,3,0,0,0,1,1,0,1,1,0,1,0,3]);
console.log('Longest Increasing Subsequence Length is ',x.length);
console.log('Original Array is\n',x.array);
console.log('One of the subarrays satisfyng longest increasing subsequence is\n',x.subarray);