From 689fda613d35f058417e4f07effa559dd82d294d Mon Sep 17 00:00:00 2001 From: ramzi Date: Sat, 11 Feb 2023 22:10:33 +0100 Subject: [PATCH] adding some sliding-window algorithms --- .../fruits into basket/README.md | 14 ++++++++ .../fruits-into-basket.js.js | 33 +++++++++++++++++++ .../maximum-subarray-sum/README.md | 17 ++++++++++ .../maximum-subarray-sum.js | 23 +++++++++++++ 4 files changed, 87 insertions(+) create mode 100644 src/algorithms/sliding-window/fruits into basket/README.md create mode 100644 src/algorithms/sliding-window/fruits into basket/fruits-into-basket.js.js create mode 100644 src/algorithms/sliding-window/maximum-subarray-sum/README.md create mode 100644 src/algorithms/sliding-window/maximum-subarray-sum/maximum-subarray-sum.js diff --git a/src/algorithms/sliding-window/fruits into basket/README.md b/src/algorithms/sliding-window/fruits into basket/README.md new file mode 100644 index 00000000..aa47ccfe --- /dev/null +++ b/src/algorithms/sliding-window/fruits into basket/README.md @@ -0,0 +1,14 @@ +# fruits into baskets (sliding window) + +- Sliding window is an algorithm/technique used to solve +subarray/subsets problems. +- It has two types : "static sliding window(size of the window is known) +and dynamic sliding(size of the window is unknown). +- It used to remove nested loops and reduce algorithm complexity to O(n). + +## link to the problem +https://leetcode.com/problems/fruit-into-baskets/ + +## References + +- [Youtube](https://www.youtube.com/watch?v=yYtaV0G3mWQ) \ No newline at end of file diff --git a/src/algorithms/sliding-window/fruits into basket/fruits-into-basket.js.js b/src/algorithms/sliding-window/fruits into basket/fruits-into-basket.js.js new file mode 100644 index 00000000..b393e14a --- /dev/null +++ b/src/algorithms/sliding-window/fruits into basket/fruits-into-basket.js.js @@ -0,0 +1,33 @@ +var totalFruit = function (arr) { + let max = 0; + let 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; +}; + +console.log(totalFruit([3, 3, 3, 1, 2, 1, 1, 2, 3, 3, 4])); diff --git a/src/algorithms/sliding-window/maximum-subarray-sum/README.md b/src/algorithms/sliding-window/maximum-subarray-sum/README.md new file mode 100644 index 00000000..a1c4303f --- /dev/null +++ b/src/algorithms/sliding-window/maximum-subarray-sum/README.md @@ -0,0 +1,17 @@ +# maximum subaray sum (sliding window) + +- Sliding window is an algorithm/technique used to solve +subarray/subsets problems. +- It has two types : "static sliding window(size of the window is known) +and dynamic sliding(size of the window is unknown). +- It used to remove nested loops and reduce algorithm complexity to O(n). + + +## link to the problem + +## References + +- [Wikipedia](https://www.scaler.com/topics/sliding-window-algorithm/#:~:text=Sliding%20Window%20Algorithm%20is%20a,result%20of%20the%20next%20step.) +- [](https://leetcode.com/problems/subarray-sum-equals-k/) +- [continuous-subarray-sum](https://leetcode.com/problems/continuous-subarray-sum/) +-[Youtube](https://www.youtube.com/watch?v=MK-NZ4hN7rs&t=1121s) \ No newline at end of file diff --git a/src/algorithms/sliding-window/maximum-subarray-sum/maximum-subarray-sum.js b/src/algorithms/sliding-window/maximum-subarray-sum/maximum-subarray-sum.js new file mode 100644 index 00000000..ff0e8a4e --- /dev/null +++ b/src/algorithms/sliding-window/maximum-subarray-sum/maximum-subarray-sum.js @@ -0,0 +1,23 @@ +//Problem : given an array of integers, return the maximum subarray sum of size k. + +function maxSubArrSum(arr, size) { + let s = 0; + let max = -Infinity; + + for (let i = 0; i < arr.length; i++) { + s += arr[i]; + if (i >= size - 1) { + if (s > max) { + max = s; + } + s -= arr[i - (size-1)]; + } + console.log(max); + } + + return max; +} + +let arr = [1, 5, 4, 8, 2, 3]; + +console.log(maxSubArrSum(arr, 3));