From 6b828a7a0cbaf9d5b7e70afe03dc8afd7ecd8600 Mon Sep 17 00:00:00 2001 From: Abhishoy Lunavat Date: Thu, 10 Oct 2019 17:42:20 +0000 Subject: [PATCH] Longest Increasing subsequence in Javascript --- .../longest-increasing-subsequence.js | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Dynamic Programming/Longest Increasing Subsequence/javascript/longest-increasing-subsequence.js diff --git a/Dynamic Programming/Longest Increasing Subsequence/javascript/longest-increasing-subsequence.js b/Dynamic Programming/Longest Increasing Subsequence/javascript/longest-increasing-subsequence.js new file mode 100644 index 000000000..1b3ad5300 --- /dev/null +++ b/Dynamic Programming/Longest Increasing Subsequence/javascript/longest-increasing-subsequence.js @@ -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 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); \ No newline at end of file