From 1606b02399a4bbe0f1424d8df2643f023d7a8a76 Mon Sep 17 00:00:00 2001 From: Catherine Cheng Date: Sat, 27 Feb 2021 18:57:58 -0800 Subject: [PATCH 1/6] fix(5.1):use bit mask and bit operators (#80) --- chapter05/5.1 - Insertion/insertion.js | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/chapter05/5.1 - Insertion/insertion.js b/chapter05/5.1 - Insertion/insertion.js index f7c05fa..350df6b 100644 --- a/chapter05/5.1 - Insertion/insertion.js +++ b/chapter05/5.1 - Insertion/insertion.js @@ -1,14 +1,11 @@ -var insertion = function(N, M, i, j) { - var n = N.split(''); - var m = M.split(''); - var nlength = n.length - 1; - var mlength = m.length - 1; - for (var a = 0; a < j - i + 1; a++) { - console.log(m[mlength - a]); - n[nlength - (i + a)] = m[mlength - a]; - } - return n.join(''); +const insertion = (N, M, i, j) => { + const bitMask = (-1 << (j + 1)) | ((1 << i) - 1); + const clearedN = N & bitMask; + const shiftedM = M << i; + return clearedN | shiftedM; }; /* TEST */ -console.log(insertion('10000000000', '10011', 2, 6), '10001001100'); \ No newline at end of file +const N = parseInt(10000000000, 2); +const M = parseInt(10011, 2); +console.log(insertion(M, N, 2, 6).toString(2), 10001001100); From bea811c3f0913359c29c62abf5493e61d8ba5c16 Mon Sep 17 00:00:00 2001 From: Carlos Green <30880055+greenc123@users.noreply.github.com> Date: Mon, 24 May 2021 10:52:40 -0700 Subject: [PATCH 2/6] 8.01 Triple step (#77) * tripleStep Backtracking, Top Down and Bottom up memoization implementation * removed comments * removed more comments * 8.01 tripleStep O(1) or constant space * removed multi-line comment * less bloating code for triple step * constant space solution * tripleStep version 2 * original and tripleStepv2 * formatted code * added TEST comment * added whitespace so both origin files are the same --- chapter08/8.01 - Triple Step/tripleStepv2.js | 64 ++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 chapter08/8.01 - Triple Step/tripleStepv2.js diff --git a/chapter08/8.01 - Triple Step/tripleStepv2.js b/chapter08/8.01 - Triple Step/tripleStepv2.js new file mode 100644 index 0000000..f694105 --- /dev/null +++ b/chapter08/8.01 - Triple Step/tripleStepv2.js @@ -0,0 +1,64 @@ +/* +Recursion and Dynamic Programming (4 steps) +1. start with the recursive backtracking solution (brute force) +2. Optimize by using a memoization table (top-down dynamic programming) +3. Remove the need for recursion (bottom-up dynamic programming) +4. Apply final tricks to reduce the time / memory complexity +*/ + +// Recursive Backtracking +// Time & Space O(n!) +function tripleStep(n, res = 0) { + if (n < 0) return 0; + if (n === 0) return 1; + return tripleStep(n + 1) + tripleStep(n - 2) + tripleStep(n - 3); +} + +// 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]; + + memo[i] = memo[i - 1] + memo[i - 2] + memo[i - 3]; + return tripleStep(n, i + 1, memo); +} + +// Bottom Up memoization +// Time & Space O(n) +function tripleStep(n, memo = [1, 1, 2, 4]) { + for (let i = 3; i <= n; i++) { + memo[i] = memo[i - 1] + memo[i - 2] + memo[i - 3]; + } + + return memo[memo.length - 1]; +} + +// Constant Space +// Time O(n) & Space O(1) +function tripleStep(n) { + if (n <= 0) return 0; + if (n === 1) return 1; + if (n === 2) return 2; + + let a = 1; + let b = 1; + let c = 2; + + for (let i = 3; i < n; i++) { + let d = a + b + c; + a = b; + b = c; + c = d; + } + return a + b + c; +} + +/* TEST */ +console.log(tripleStep(1), 1); +console.log(tripleStep(2), 2); +console.log(tripleStep(3), 4); +console.log(tripleStep(5), 13); +console.log(tripleStep(7), 44); +console.log(tripleStep(10), 274); From 05e701b1ef12a52a9c87e401d39f8751ea7e279d Mon Sep 17 00:00:00 2001 From: Kapil Raghuwanshi Date: Wed, 2 Jun 2021 21:01:07 +0530 Subject: [PATCH 3/6] 3.1: Removed unused value in pop3() operation (#82) Line no 47 --- chapter03/3.1 - Three in One/threeInOne.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/chapter03/3.1 - Three in One/threeInOne.js b/chapter03/3.1 - Three in One/threeInOne.js index 147b6c7..f7917c4 100644 --- a/chapter03/3.1 - Three in One/threeInOne.js +++ b/chapter03/3.1 - Three in One/threeInOne.js @@ -44,12 +44,12 @@ ThreeInOne.prototype.pop2 = function() { return answer; }; -ThreeInOne.prototype.pop3 = function(value) { +ThreeInOne.prototype.pop3 = function() { if (this.isEmpty3()) { return undefined; } - return this.container.pop(value); + return this.container.pop(); }; ThreeInOne.prototype.peek1 = function() { From 88b95fd0c86b0b137a7f5e60fecb2a2ba7f66b8e Mon Sep 17 00:00:00 2001 From: Carlos Green Date: Sat, 28 Jan 2023 11:06:28 -0800 Subject: [PATCH 4/6] 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 5/6] 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 6/6] 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]