From 88b95fd0c86b0b137a7f5e60fecb2a2ba7f66b8e Mon Sep 17 00:00:00 2001 From: Carlos Green Date: Sat, 28 Jan 2023 11:06:28 -0800 Subject: [PATCH 1/3] fix: remove bloat in triple step (#86) --- chapter08/8.01 - Triple Step/tripleStepv2.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/chapter08/8.01 - Triple Step/tripleStepv2.js b/chapter08/8.01 - Triple Step/tripleStepv2.js index f694105..e41f6b3 100644 --- a/chapter08/8.01 - Triple Step/tripleStepv2.js +++ b/chapter08/8.01 - Triple Step/tripleStepv2.js @@ -8,7 +8,7 @@ Recursion and Dynamic Programming (4 steps) // Recursive Backtracking // Time & Space O(n!) -function tripleStep(n, res = 0) { +function tripleStep(n) { if (n < 0) return 0; if (n === 0) return 1; return tripleStep(n + 1) + tripleStep(n - 2) + tripleStep(n - 3); @@ -17,10 +17,8 @@ function tripleStep(n, res = 0) { // Top Down Memoization // Time & Space O(n) function tripleStep(n, i = 3, memo = [1, 1, 2, 4]) { - if (n < 0) return memo[0]; - if (n === 1) return memo[n]; - if (i > n) return memo[memo.length - 1]; - + if (n < 0) return 0 + if (n < i) return memo[n]; memo[i] = memo[i - 1] + memo[i - 2] + memo[i - 3]; return tripleStep(n, i + 1, memo); } From 9553d4754ebaf92b88502322a1cadd09705acbc4 Mon Sep 17 00:00:00 2001 From: Kapil Raghuwanshi Date: Sun, 29 Jan 2023 00:41:47 +0530 Subject: [PATCH 2/3] fix: replaced "n+1" to "n-1" in triple-step (#88) It could be a typo but wrong solution. --- chapter08/8.01 - Triple Step/tripleStepv2.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chapter08/8.01 - Triple Step/tripleStepv2.js b/chapter08/8.01 - Triple Step/tripleStepv2.js index e41f6b3..32ae4d8 100644 --- a/chapter08/8.01 - Triple Step/tripleStepv2.js +++ b/chapter08/8.01 - Triple Step/tripleStepv2.js @@ -11,7 +11,7 @@ Recursion and Dynamic Programming (4 steps) function tripleStep(n) { if (n < 0) return 0; if (n === 0) return 1; - return tripleStep(n + 1) + tripleStep(n - 2) + tripleStep(n - 3); + return tripleStep(n - 1) + tripleStep(n - 2) + tripleStep(n - 3); } // Top Down Memoization From 33830739629d3d3f9a4b0f122ba5150431a9d506 Mon Sep 17 00:00:00 2001 From: Kapil Raghuwanshi Date: Sun, 29 Jan 2023 00:44:07 +0530 Subject: [PATCH 3/3] new soln: find duplicate, 10.08 (#84) --- .../10.08 - Find Duplicates/findDuplicates.js | 51 ++++++++++++++++++- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/chapter10/10.08 - Find Duplicates/findDuplicates.js b/chapter10/10.08 - Find Duplicates/findDuplicates.js index 07bfb1c..1562e64 100644 --- a/chapter10/10.08 - Find Duplicates/findDuplicates.js +++ b/chapter10/10.08 - Find Duplicates/findDuplicates.js @@ -1,2 +1,49 @@ -// sort numbers -// stream through numbers and print elements which is a duplicate of the immediate previous number +// We're given 4 kilobytes (4 * 2^10 * 8 = 32*2^10 bits) of main memory(RAM) to write a program to print all duplicate entries in an array +// from 1 to N where N is at most 32,000 (32*2^10). + +// We can use an Int8Array that uses 1 Byte per element to store 0's if we haven't seen it and 1's if we have seen it. +// The Int8Array typed array represents an array of twos-complement 8-bit signed integers. +// 4 KB > 32,000 bits + +// Time Complexity - O(n) +// Space Complexity - O(n) + +// Let's have a JavaScript Solution of this program: + +function FindDuplicates(arr, range = 32000) { + // Initialize integer 8 bit array with 0's at every index + const numbersSeen = new Int8Array(range); + const duplicates = []; + for (let i = 0; i < arr.length; i++) { + if (numbersSeen[arr[i]] === 0) { + numbersSeen[arr[i]] = 1; + } else { + duplicates.push(arr[i]); + } + } + return duplicates; +} + + const arr = [ + 0, + 1, + 1, + 2, + 4, + 6, + 7, + 9, + 9, + 34, + 56, + 78, + 90, + 101, + 345, + 789, + 999, + 999, + 11000 + ]; + FindDuplicates(arr); +// [1, 9, 999]