From c4819fc515ea3fc44e2d41f9575b42f155567272 Mon Sep 17 00:00:00 2001 From: ramzi Date: Tue, 14 Feb 2023 17:28:41 +0100 Subject: [PATCH 1/6] adding sliding window problem(fruitsIntoBasket) --- .../Sliding-Window/FruitsIntoBasket.js | 37 +++++++++++++++++++ .../test/FruitsIntoBasket.test.js | 5 +++ 2 files changed, 42 insertions(+) create mode 100644 Dynamic-Programming/Sliding-Window/FruitsIntoBasket.js create mode 100644 Dynamic-Programming/Sliding-Window/test/FruitsIntoBasket.test.js diff --git a/Dynamic-Programming/Sliding-Window/FruitsIntoBasket.js b/Dynamic-Programming/Sliding-Window/FruitsIntoBasket.js new file mode 100644 index 0000000000..de19cc7a2d --- /dev/null +++ b/Dynamic-Programming/Sliding-Window/FruitsIntoBasket.js @@ -0,0 +1,37 @@ +/** + * @param {number[]} arr + * @return {number} res + * @see https://leetcode.com/problems/fruit-into-baskets/ + * @see [sliding-window-technique] (https://www.geeksforgeeks.org/window-sliding-technique/) + */ + +export const totalFruit = function (arr) { + let max = 0 + const hash = {} + let j = 0 + let objLength = 0 + + for (let i = 0; i < arr.length; i++) { + if (!hash[arr[i]]) { + hash[arr[i]] = 1 + objLength++ + } else { + hash[arr[i]]++ + } + + if (objLength <= 2) { + max = Math.max(max, i - j + 1) + } else { + while (objLength > 2) { + hash[arr[j]]-- + if (hash[arr[j]] === 0) { + delete hash[arr[j]] + objLength-- + } + j++ + } + } + } + + return max +} diff --git a/Dynamic-Programming/Sliding-Window/test/FruitsIntoBasket.test.js b/Dynamic-Programming/Sliding-Window/test/FruitsIntoBasket.test.js new file mode 100644 index 0000000000..5473c4f6ba --- /dev/null +++ b/Dynamic-Programming/Sliding-Window/test/FruitsIntoBasket.test.js @@ -0,0 +1,5 @@ +import { totalFruit } from '../FruitsIntoBasket' + +test('generate all valid parentheses of input n', () => { + expect(totalFruit([3, 3, 3, 1, 2, 1, 1, 2, 3, 3, 4])).toStrictEqual(5) +}) From b4c0075914c73c5547c7259a6edd329224593652 Mon Sep 17 00:00:00 2001 From: ramzi Date: Tue, 14 Feb 2023 17:29:42 +0100 Subject: [PATCH 2/6] adding sliding window problem(fruitsIntoBasket) --- .../Sliding-Window/test/FruitsIntoBasket.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dynamic-Programming/Sliding-Window/test/FruitsIntoBasket.test.js b/Dynamic-Programming/Sliding-Window/test/FruitsIntoBasket.test.js index 5473c4f6ba..68a9c107cf 100644 --- a/Dynamic-Programming/Sliding-Window/test/FruitsIntoBasket.test.js +++ b/Dynamic-Programming/Sliding-Window/test/FruitsIntoBasket.test.js @@ -1,5 +1,5 @@ import { totalFruit } from '../FruitsIntoBasket' -test('generate all valid parentheses of input n', () => { +test('result : ', () => { expect(totalFruit([3, 3, 3, 1, 2, 1, 1, 2, 3, 3, 4])).toStrictEqual(5) }) From ac93811ae9ec835c8c6a4863bf516207ba63db0d Mon Sep 17 00:00:00 2001 From: ramzi Date: Tue, 14 Feb 2023 21:40:55 +0100 Subject: [PATCH 3/6] adding more test cases and comments --- Dynamic-Programming/Sliding-Window/FruitsIntoBasket.js | 5 +++++ .../Sliding-Window/test/FruitsIntoBasket.test.js | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/Dynamic-Programming/Sliding-Window/FruitsIntoBasket.js b/Dynamic-Programming/Sliding-Window/FruitsIntoBasket.js index de19cc7a2d..60bcc7a160 100644 --- a/Dynamic-Programming/Sliding-Window/FruitsIntoBasket.js +++ b/Dynamic-Programming/Sliding-Window/FruitsIntoBasket.js @@ -1,6 +1,11 @@ /** + * @function totalFruit * @param {number[]} arr * @return {number} res + * @name sliding-window this technique is used to when you have a subsets/subsequence problem, it's efficient in terms of time complexity, + * since it reduces time complexity to O(n). + * @see hints for sliding window problems: + * when you see keywords like: subarrays, maximum/minimum subarray, longest substring/subsequence, etc * @see https://leetcode.com/problems/fruit-into-baskets/ * @see [sliding-window-technique] (https://www.geeksforgeeks.org/window-sliding-technique/) */ diff --git a/Dynamic-Programming/Sliding-Window/test/FruitsIntoBasket.test.js b/Dynamic-Programming/Sliding-Window/test/FruitsIntoBasket.test.js index 68a9c107cf..13463c14b2 100644 --- a/Dynamic-Programming/Sliding-Window/test/FruitsIntoBasket.test.js +++ b/Dynamic-Programming/Sliding-Window/test/FruitsIntoBasket.test.js @@ -2,4 +2,8 @@ import { totalFruit } from '../FruitsIntoBasket' test('result : ', () => { expect(totalFruit([3, 3, 3, 1, 2, 1, 1, 2, 3, 3, 4])).toStrictEqual(5) + expect(totalFruit([5, 3, 3, 1, 2])).toStrictEqual(3) + expect(totalFruit([9, 1, 2, 2, 2])).toStrictEqual(4) + expect(totalFruit([3, 3, 3])).toStrictEqual(3) + expect(totalFruit([])).toStrictEqual(0) }) From 6d51c5000d5b895ee310b01f5ef382987d3bd44d Mon Sep 17 00:00:00 2001 From: ramzi Date: Thu, 16 Feb 2023 21:28:04 +0100 Subject: [PATCH 4/6] adding comments to the algo --- .../Sliding-Window/FruitsIntoBasket.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Dynamic-Programming/Sliding-Window/FruitsIntoBasket.js b/Dynamic-Programming/Sliding-Window/FruitsIntoBasket.js index 60bcc7a160..8ffec2917d 100644 --- a/Dynamic-Programming/Sliding-Window/FruitsIntoBasket.js +++ b/Dynamic-Programming/Sliding-Window/FruitsIntoBasket.js @@ -2,33 +2,37 @@ * @function totalFruit * @param {number[]} arr * @return {number} res - * @name sliding-window this technique is used to when you have a subsets/subsequence problem, it's efficient in terms of time complexity, - * since it reduces time complexity to O(n). - * @see hints for sliding window problems: - * when you see keywords like: subarrays, maximum/minimum subarray, longest substring/subsequence, etc * @see https://leetcode.com/problems/fruit-into-baskets/ * @see [sliding-window-technique] (https://www.geeksforgeeks.org/window-sliding-technique/) */ export const totalFruit = function (arr) { + // to maximize number of elements that our basket can have let max = 0 + // keeps track of the occurence of each element const hash = {} + // left side of the window let j = 0 + // count number of elements let objLength = 0 for (let i = 0; i < arr.length; i++) { + // A hashmap keeps track of each character and it's occurence if (!hash[arr[i]]) { hash[arr[i]] = 1 + // when we have a new element we increment objLength objLength++ } else { hash[arr[i]]++ } - if (objLength <= 2) { + // maximizing and opening the window max = Math.max(max, i - j + 1) } else { + // once we have more than two distinct elements in our hashmap, we should shrink our window while (objLength > 2) { hash[arr[j]]-- + // when the occurence of an element becomes 0 we should delete it from the hashmap if (hash[arr[j]] === 0) { delete hash[arr[j]] objLength-- From 25ae758723094df3c6387ae749fcbae25c91efa7 Mon Sep 17 00:00:00 2001 From: ramzi Date: Thu, 16 Feb 2023 21:44:10 +0100 Subject: [PATCH 5/6] adding comments to the algo --- Dynamic-Programming/Sliding-Window/FruitsIntoBasket.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dynamic-Programming/Sliding-Window/FruitsIntoBasket.js b/Dynamic-Programming/Sliding-Window/FruitsIntoBasket.js index 8ffec2917d..af8c457349 100644 --- a/Dynamic-Programming/Sliding-Window/FruitsIntoBasket.js +++ b/Dynamic-Programming/Sliding-Window/FruitsIntoBasket.js @@ -9,11 +9,11 @@ export const totalFruit = function (arr) { // to maximize number of elements that our basket can have let max = 0 - // keeps track of the occurence of each element + // keeps track of the occurrence of each element const hash = {} // left side of the window let j = 0 - // count number of elements + // to count number of elements let objLength = 0 for (let i = 0; i < arr.length; i++) { From 01e06f525386abf670d19e7563bbfd252aced610 Mon Sep 17 00:00:00 2001 From: ramzi Date: Thu, 16 Feb 2023 21:54:03 +0100 Subject: [PATCH 6/6] adding comments to the algo, coorecting spelling errors --- Dynamic-Programming/Sliding-Window/FruitsIntoBasket.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dynamic-Programming/Sliding-Window/FruitsIntoBasket.js b/Dynamic-Programming/Sliding-Window/FruitsIntoBasket.js index af8c457349..695f7a2b8a 100644 --- a/Dynamic-Programming/Sliding-Window/FruitsIntoBasket.js +++ b/Dynamic-Programming/Sliding-Window/FruitsIntoBasket.js @@ -17,7 +17,7 @@ export const totalFruit = function (arr) { let objLength = 0 for (let i = 0; i < arr.length; i++) { - // A hashmap keeps track of each character and it's occurence + // A hashmap keeps track of each character and it's occurrence if (!hash[arr[i]]) { hash[arr[i]] = 1 // when we have a new element we increment objLength @@ -32,7 +32,7 @@ export const totalFruit = function (arr) { // once we have more than two distinct elements in our hashmap, we should shrink our window while (objLength > 2) { hash[arr[j]]-- - // when the occurence of an element becomes 0 we should delete it from the hashmap + // when the occurrence of an element becomes 0 we should delete it from the hashmap if (hash[arr[j]] === 0) { delete hash[arr[j]] objLength--