From 28502696746fbc61c5d6a095f5f8f554f7a327f5 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Sun, 4 Oct 2020 00:49:18 +0530 Subject: [PATCH 1/8] Added Longest Common Subsequence --- .../longestCommonSubsequence.js | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Dynamic-Programming/longestCommonSubsequence.js diff --git a/Dynamic-Programming/longestCommonSubsequence.js b/Dynamic-Programming/longestCommonSubsequence.js new file mode 100644 index 0000000000..f4e0c7fb2e --- /dev/null +++ b/Dynamic-Programming/longestCommonSubsequence.js @@ -0,0 +1,31 @@ +/* + * Given two sequences, find the length of longest subsequence present in both of them. + * A subsequence is a sequence that appears in the same relative order, but not necessarily contiguous. + * For example, “abc”, “abg”, “bdf”, “aeg”, ‘”acefg”, .. etc are subsequences of “abcdefg” +*/ + +function longestCommonSubsequence(x, y, str1, str2, dp) { + if (x == -1 || y == -1) return 0; + else { + if (dp[x][y] != 0) return dp[x][y]; + else { + if (str1[x] == str2[y]) { + return dp[x][y] = 1 + longestCommonSubsequence(x - 1, y - 1, str1, str2, dp); + } + else { + return dp[x][y] = Math.max(longestCommonSubsequence(x - 1, y, str1, str2, dp), longestCommonSubsequence(x, y - 1, str1, str2, dp)) + } + } + } + +} + +function main() { + const str1 = "ABCDGH" + const str2 = "AEDFHR" + let dp = new Array(str1.length + 1).fill(0).map(x => new Array(str2.length + 1).fill(0)) + const res = longestCommonSubsequence(str1.length - 1, str2.length - 1, str1, str2, dp) + console.log(res); +} + +main() From 595319b65160ee93de83ba5fa7b58439661d3d45 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Sun, 4 Oct 2020 00:53:31 +0530 Subject: [PATCH 2/8] Renamed the File --- .../{longestCommonSubsequence.js => LongestCommonSubsequence.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Dynamic-Programming/{longestCommonSubsequence.js => LongestCommonSubsequence.js} (100%) diff --git a/Dynamic-Programming/longestCommonSubsequence.js b/Dynamic-Programming/LongestCommonSubsequence.js similarity index 100% rename from Dynamic-Programming/longestCommonSubsequence.js rename to Dynamic-Programming/LongestCommonSubsequence.js From b8a519399a8a3ba71aec47c36892865afbeb9e8f Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Sun, 4 Oct 2020 00:59:07 +0530 Subject: [PATCH 3/8] Optimized the code --- Dynamic-Programming/LongestCommonSubsequence.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dynamic-Programming/LongestCommonSubsequence.js b/Dynamic-Programming/LongestCommonSubsequence.js index f4e0c7fb2e..ac5e61e3ab 100644 --- a/Dynamic-Programming/LongestCommonSubsequence.js +++ b/Dynamic-Programming/LongestCommonSubsequence.js @@ -23,7 +23,7 @@ function longestCommonSubsequence(x, y, str1, str2, dp) { function main() { const str1 = "ABCDGH" const str2 = "AEDFHR" - let dp = new Array(str1.length + 1).fill(0).map(x => new Array(str2.length + 1).fill(0)) + const dp = new Array(str1.length + 1).fill(0).map(x => new Array(str2.length + 1).fill(0)) const res = longestCommonSubsequence(str1.length - 1, str2.length - 1, str1, str2, dp) console.log(res); } From a56758580db98fb3564846bbd61f611d07525239 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Sun, 4 Oct 2020 01:06:25 +0530 Subject: [PATCH 4/8] Optimized the code --- .../LongestCommonSubsequence.js | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/Dynamic-Programming/LongestCommonSubsequence.js b/Dynamic-Programming/LongestCommonSubsequence.js index ac5e61e3ab..9ba2edbbb0 100644 --- a/Dynamic-Programming/LongestCommonSubsequence.js +++ b/Dynamic-Programming/LongestCommonSubsequence.js @@ -1,31 +1,36 @@ /* - * Given two sequences, find the length of longest subsequence present in both of them. - * A subsequence is a sequence that appears in the same relative order, but not necessarily contiguous. + * Given two sequences, find the length of longest subsequence present in both of them. + * A subsequence is a sequence that appears in the same relative order, but not necessarily contiguous. * For example, “abc”, “abg”, “bdf”, “aeg”, ‘”acefg”, .. etc are subsequences of “abcdefg” */ -function longestCommonSubsequence(x, y, str1, str2, dp) { - if (x == -1 || y == -1) return 0; +function longestCommonSubsequence (x, y, str1, str2, dp) { + if (x === -1 || y === -1) { + return 0 + } else { - if (dp[x][y] != 0) return dp[x][y]; + if (dp[x][y] !== 0){ + return dp[x][y] + } else { - if (str1[x] == str2[y]) { - return dp[x][y] = 1 + longestCommonSubsequence(x - 1, y - 1, str1, str2, dp); + if (str1[x] === str2[y]) { + dp[x][y] = 1 + longestCommonSubsequence(x - 1, y - 1, str1, str2, dp); + return dp[x][y] } else { - return dp[x][y] = Math.max(longestCommonSubsequence(x - 1, y, str1, str2, dp), longestCommonSubsequence(x, y - 1, str1, str2, dp)) + dp[x][y] = Math.max(longestCommonSubsequence(x - 1, y, str1, str2, dp), longestCommonSubsequence(x, y - 1, str1, str2, dp)) + return dp[x][y] } } } - } -function main() { - const str1 = "ABCDGH" - const str2 = "AEDFHR" +function main () { + const str1 = 'ABCDGH' + const str2 = 'AEDFHR' const dp = new Array(str1.length + 1).fill(0).map(x => new Array(str2.length + 1).fill(0)) const res = longestCommonSubsequence(str1.length - 1, str2.length - 1, str1, str2, dp) - console.log(res); + console.log(res) } main() From fdc34c32dca3fe4381876693c448e0a599bf6dd8 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Sun, 4 Oct 2020 01:13:17 +0530 Subject: [PATCH 5/8] Changed some styles as per the rule --- Dynamic-Programming/LongestCommonSubsequence.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Dynamic-Programming/LongestCommonSubsequence.js b/Dynamic-Programming/LongestCommonSubsequence.js index 9ba2edbbb0..b5f0287696 100644 --- a/Dynamic-Programming/LongestCommonSubsequence.js +++ b/Dynamic-Programming/LongestCommonSubsequence.js @@ -11,13 +11,12 @@ function longestCommonSubsequence (x, y, str1, str2, dp) { else { if (dp[x][y] !== 0){ return dp[x][y] - } + } else { if (str1[x] === str2[y]) { - dp[x][y] = 1 + longestCommonSubsequence(x - 1, y - 1, str1, str2, dp); + dp[x][y] = 1 + longestCommonSubsequence(x - 1, y - 1, str1, str2, dp) return dp[x][y] - } - else { + } else { dp[x][y] = Math.max(longestCommonSubsequence(x - 1, y, str1, str2, dp), longestCommonSubsequence(x, y - 1, str1, str2, dp)) return dp[x][y] } From e9fe0ce598fe81939cda5583a9523b07c8999d0c Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Sun, 4 Oct 2020 01:16:33 +0530 Subject: [PATCH 6/8] Again some style fixed --- Dynamic-Programming/LongestCommonSubsequence.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Dynamic-Programming/LongestCommonSubsequence.js b/Dynamic-Programming/LongestCommonSubsequence.js index b5f0287696..f4724ccb08 100644 --- a/Dynamic-Programming/LongestCommonSubsequence.js +++ b/Dynamic-Programming/LongestCommonSubsequence.js @@ -7,12 +7,10 @@ function longestCommonSubsequence (x, y, str1, str2, dp) { if (x === -1 || y === -1) { return 0 - } - else { - if (dp[x][y] !== 0){ + } else { + if (dp[x][y] !== 0) { return dp[x][y] - } - else { + } else { if (str1[x] === str2[y]) { dp[x][y] = 1 + longestCommonSubsequence(x - 1, y - 1, str1, str2, dp) return dp[x][y] From 838abade98e4ebbd79482ee3ca2c29abbe3eb2c5 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Sun, 4 Oct 2020 11:47:48 +0530 Subject: [PATCH 7/8] Added Longest Increasing Subsequence program to the list --- .../LongestIncreasingSubsequence.js | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Dynamic-Programming/LongestIncreasingSubsequence.js diff --git a/Dynamic-Programming/LongestIncreasingSubsequence.js b/Dynamic-Programming/LongestIncreasingSubsequence.js new file mode 100644 index 0000000000..1ea8dde65a --- /dev/null +++ b/Dynamic-Programming/LongestIncreasingSubsequence.js @@ -0,0 +1,26 @@ +/** + * A Dynamic Programming based solution for calculating Longest Increasing Subsequence + * https://en.wikipedia.org/wiki/Longest_increasing_subsequence + */ + +function main () { + const x = [0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15] + const length = x.length + const dp = Array(length).fill(1) + + let res = 1 + + for (let i = 0; i < length; i++) { + for (let j = 0; j < i; j++) { + if (x[i] > x[j]) { + dp[i] = Math.max(dp[i], 1 + dp[j]) + if (dp[i] > res) + res = dp[i] + } + } + } + + console.log('Length of Longest Increasing Subsequence is:', res) +} + +main() From 87b2eb02923c3e6d154331ec85b56cded052f5da Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Sun, 4 Oct 2020 11:51:10 +0530 Subject: [PATCH 8/8] Style changed --- Dynamic-Programming/LongestIncreasingSubsequence.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Dynamic-Programming/LongestIncreasingSubsequence.js b/Dynamic-Programming/LongestIncreasingSubsequence.js index 1ea8dde65a..43cadecce9 100644 --- a/Dynamic-Programming/LongestIncreasingSubsequence.js +++ b/Dynamic-Programming/LongestIncreasingSubsequence.js @@ -14,12 +14,13 @@ function main () { for (let j = 0; j < i; j++) { if (x[i] > x[j]) { dp[i] = Math.max(dp[i], 1 + dp[j]) - if (dp[i] > res) + if (dp[i] > res) { res = dp[i] + } } } } - + console.log('Length of Longest Increasing Subsequence is:', res) }