From 28502696746fbc61c5d6a095f5f8f554f7a327f5 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Sun, 4 Oct 2020 00:49:18 +0530 Subject: [PATCH 01/13] 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 02/13] 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 03/13] 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 04/13] 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 05/13] 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 06/13] 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 07/13] 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 08/13] 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) } From 8a40a08f3e65da652e011db4232ddcccd8859316 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Sun, 4 Oct 2020 22:22:14 +0530 Subject: [PATCH 09/13] Added 0-1-Knapsack Problem --- Dynamic-Programming/ZeroOneKnapsack.js | 71 ++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 Dynamic-Programming/ZeroOneKnapsack.js diff --git a/Dynamic-Programming/ZeroOneKnapsack.js b/Dynamic-Programming/ZeroOneKnapsack.js new file mode 100644 index 0000000000..f9cc2d450a --- /dev/null +++ b/Dynamic-Programming/ZeroOneKnapsack.js @@ -0,0 +1,71 @@ +/** + * A Dynamic Programming based solution for calculating Zero One Knapsack + * https://en.wikipedia.org/wiki/Knapsack_problem + */ + +function zeroOneKnapsack (arr, n, cap, cache) { + if (cap == 0 || n == 0) { + cache[n][cap] = 0 + return cache[n][cap] + } + if (cache[n][cap] != -1) { + return cache[n][cap] + } + if (arr[n - 1][0] <= cap) { + cache[n][cap] = Math.max(arr[n - 1][1] + zeroOneKnapsack (arr, n - 1, cap - arr[n - 1][0], cache), zeroOneKnapsack (arr, n - 1, cap, cache)) + return cache[n][cap] + } else { + cache[n][cap] = zeroOneKnapsack (arr, n - 1, cap, cache) + return cache[n][cap] + } +} + +function main () { + /* + Problem Statement: + You are a thief carrying a single bag with limited capacity S. The museum you stole had N artifact that you could steal. Unfortunately you might not be able to steal all the artifact because of your limited bag capacity. + You have to cherry pick the artifact in order to maximize the total value of the artifacts you stole. + + Link for the Problem: https://www.hackerrank.com/contests/srin-aadc03/challenges/classic-01-knapsack + */ + let input = `1 + 4 5 + 1 8 + 2 4 + 3 0 + 2 5 + 2 3` + + input = input.trim().split('\n') + input.shift() + const length = input.length + + let i = 0 + while (i < length) { + const cap = Number(input[i].trim().split(' ')[0]) + const currlen = Number(input[i].trim().split(' ')[1]) + let j = i + 1; + const arr = [] + while (j <= i + currlen) { + arr.push(input[j]) + j++ + } + const newArr = [] + arr.map(e => { + newArr.push(e.trim().split(' ').map(Number)) + }) + const cache = [] + for (let i = 0; i <= currlen; i++) { + const temp = [] + for (let j = 0; j <= cap; j++) { + temp.push(-1) + } + cache.push(temp) + } + const result = zeroOneKnapsack(newArr, currlen, cap, cache) + console.log(result) + i += currlen + 1 + } +} + +main() \ No newline at end of file From 7a65d8929fdecb79979b5a3ef6698d7573dfcc42 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Sun, 4 Oct 2020 22:27:53 +0530 Subject: [PATCH 10/13] Style Changed as per guidelines --- Dynamic-Programming/ZeroOneKnapsack.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Dynamic-Programming/ZeroOneKnapsack.js b/Dynamic-Programming/ZeroOneKnapsack.js index f9cc2d450a..e418db1aee 100644 --- a/Dynamic-Programming/ZeroOneKnapsack.js +++ b/Dynamic-Programming/ZeroOneKnapsack.js @@ -4,18 +4,18 @@ */ function zeroOneKnapsack (arr, n, cap, cache) { - if (cap == 0 || n == 0) { + if (cap === 0 || n === 0) { cache[n][cap] = 0 return cache[n][cap] } - if (cache[n][cap] != -1) { + if (cache[n][cap] !== -1) { return cache[n][cap] } if (arr[n - 1][0] <= cap) { - cache[n][cap] = Math.max(arr[n - 1][1] + zeroOneKnapsack (arr, n - 1, cap - arr[n - 1][0], cache), zeroOneKnapsack (arr, n - 1, cap, cache)) + cache[n][cap] = Math.max(arr[n - 1][1] + zeroOneKnapsack(arr, n - 1, cap - arr[n - 1][0], cache), zeroOneKnapsack(arr, n - 1, cap, cache)) return cache[n][cap] } else { - cache[n][cap] = zeroOneKnapsack (arr, n - 1, cap, cache) + cache[n][cap] = zeroOneKnapsack(arr, n - 1, cap, cache) return cache[n][cap] } } @@ -44,7 +44,7 @@ function main () { while (i < length) { const cap = Number(input[i].trim().split(' ')[0]) const currlen = Number(input[i].trim().split(' ')[1]) - let j = i + 1; + let j = i + 1 const arr = [] while (j <= i + currlen) { arr.push(input[j]) @@ -68,4 +68,4 @@ function main () { } } -main() \ No newline at end of file +main() From 073b9cebd9624d20c7c6898059df20442f161aa4 Mon Sep 17 00:00:00 2001 From: vinayak Date: Sun, 4 Oct 2020 23:02:02 +0530 Subject: [PATCH 11/13] Update ZeroOneKnapsack.js --- Dynamic-Programming/ZeroOneKnapsack.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dynamic-Programming/ZeroOneKnapsack.js b/Dynamic-Programming/ZeroOneKnapsack.js index e418db1aee..0274fdefdb 100644 --- a/Dynamic-Programming/ZeroOneKnapsack.js +++ b/Dynamic-Programming/ZeroOneKnapsack.js @@ -3,7 +3,7 @@ * https://en.wikipedia.org/wiki/Knapsack_problem */ -function zeroOneKnapsack (arr, n, cap, cache) { +const zeroOneKnapsack = (arr, n, cap, cache) => { if (cap === 0 || n === 0) { cache[n][cap] = 0 return cache[n][cap] @@ -20,7 +20,7 @@ function zeroOneKnapsack (arr, n, cap, cache) { } } -function main () { +const main = () => { /* Problem Statement: You are a thief carrying a single bag with limited capacity S. The museum you stole had N artifact that you could steal. Unfortunately you might not be able to steal all the artifact because of your limited bag capacity. From 8d89f355cd936d65fe84198d83007c2525a01ac8 Mon Sep 17 00:00:00 2001 From: vinayak Date: Sun, 4 Oct 2020 23:02:58 +0530 Subject: [PATCH 12/13] Delete LongestCommonSubsequence.js --- .../LongestCommonSubsequence.js | 33 ------------------- 1 file changed, 33 deletions(-) delete mode 100644 Dynamic-Programming/LongestCommonSubsequence.js diff --git a/Dynamic-Programming/LongestCommonSubsequence.js b/Dynamic-Programming/LongestCommonSubsequence.js deleted file mode 100644 index f4724ccb08..0000000000 --- a/Dynamic-Programming/LongestCommonSubsequence.js +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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]) { - dp[x][y] = 1 + longestCommonSubsequence(x - 1, y - 1, str1, str2, dp) - return dp[x][y] - } else { - 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' - 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) -} - -main() From 872d60f094fd47c8571484de9631d860a92f16ad Mon Sep 17 00:00:00 2001 From: vinayak Date: Sun, 4 Oct 2020 23:03:13 +0530 Subject: [PATCH 13/13] Delete LongestIncreasingSubsequence.js --- .../LongestIncreasingSubsequence.js | 27 ------------------- 1 file changed, 27 deletions(-) delete mode 100644 Dynamic-Programming/LongestIncreasingSubsequence.js diff --git a/Dynamic-Programming/LongestIncreasingSubsequence.js b/Dynamic-Programming/LongestIncreasingSubsequence.js deleted file mode 100644 index 43cadecce9..0000000000 --- a/Dynamic-Programming/LongestIncreasingSubsequence.js +++ /dev/null @@ -1,27 +0,0 @@ -/** - * 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() pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy