From 2e2361e496eae35582208596ec293c4f747a836c Mon Sep 17 00:00:00 2001 From: k7 Date: Tue, 5 Nov 2024 22:27:57 +0530 Subject: [PATCH 1/2] feat: implement sliding window algorithms for fixed and dynamic sizes with tests --- .../LongestSubarrayWithSumAtMost.js | 21 +++++++++++++++ Sliding-Windows/MaxSumSubarrayFixed.js | 26 +++++++++++++++++++ .../test/LongestSubarrayWithSumAtMost.test.js | 24 +++++++++++++++++ .../test/MaxSumSubarrayFixed.test.js | 22 ++++++++++++++++ 4 files changed, 93 insertions(+) create mode 100644 Sliding-Windows/LongestSubarrayWithSumAtMost.js create mode 100644 Sliding-Windows/MaxSumSubarrayFixed.js create mode 100644 Sliding-Windows/test/LongestSubarrayWithSumAtMost.test.js create mode 100644 Sliding-Windows/test/MaxSumSubarrayFixed.test.js diff --git a/Sliding-Windows/LongestSubarrayWithSumAtMost.js b/Sliding-Windows/LongestSubarrayWithSumAtMost.js new file mode 100644 index 0000000000..b970539ee0 --- /dev/null +++ b/Sliding-Windows/LongestSubarrayWithSumAtMost.js @@ -0,0 +1,21 @@ +/** + * Function to find the longest subarray with a sum <= target. + * + * @param {number[]} arr - The input array of numbers. + * @param {number} target - The target sum for the dynamic window. + * @returns {number} - The length of the longest subarray with a sum <= target. + */ +export function longestSubarrayWithSumAtMost(arr, target) { + let maxLength = 0 + let windowSum = 0 + let left = 0 + for (let right = 0; right < arr.length; right++) { + windowSum += arr[right] + while (windowSum > target) { + windowSum -= arr[left] + left++ + } + maxLength = Math.max(maxLength, right - left + 1) + } + return maxLength +} diff --git a/Sliding-Windows/MaxSumSubarrayFixed.js b/Sliding-Windows/MaxSumSubarrayFixed.js new file mode 100644 index 0000000000..ba8b844f14 --- /dev/null +++ b/Sliding-Windows/MaxSumSubarrayFixed.js @@ -0,0 +1,26 @@ +/** + * Function to find the maximum sum of a subarray of fixed size k. + * + * @param {number[]} arr - The input array of numbers. + * @param {number} k - The fixed size of the subarray. + * @returns {number} - The maximum sum of any subarray of size k. + * @throws {RangeError} - If k is larger than the array length or less than 1. + */ +export function maxSumSubarrayFixed(arr, k) { + if (k > arr.length || k < 1) { + throw new RangeError( + 'Subarray size k must be between 1 and the length of the array' + ) + } + let maxSum = 0 + let windowSum = 0 + for (let i = 0; i < k; i++) { + windowSum += arr[i] + } + maxSum = windowSum + for (let i = k; i < arr.length; i++) { + windowSum += arr[i] - arr[i - k] + maxSum = Math.max(maxSum, windowSum) + } + return maxSum +} diff --git a/Sliding-Windows/test/LongestSubarrayWithSumAtMost.test.js b/Sliding-Windows/test/LongestSubarrayWithSumAtMost.test.js new file mode 100644 index 0000000000..c33188fffb --- /dev/null +++ b/Sliding-Windows/test/LongestSubarrayWithSumAtMost.test.js @@ -0,0 +1,24 @@ +import { longestSubarrayWithSumAtMost } from '../LongestSubarrayWithSumAtMost' + +describe('Dynamic-size Sliding Window - longestSubarrayWithSumAtMost', () => { + it('should return the longest subarray length with sum <= target', () => { + const arr = [1, 2, 3, 4, 5] + const target = 7 + const result = longestSubarrayWithSumAtMost(arr, target) + expect(result).toBe(3) + }) + + it('should return the full array length if the entire sum is within the target', () => { + const arr = [1, 1, 1, 1] + const target = 4 + const result = longestSubarrayWithSumAtMost(arr, target) + expect(result).toBe(4) + }) + + it('should return 0 if no subarray meets the sum condition', () => { + const arr = [5, 6, 7] + const target = 3 + const result = longestSubarrayWithSumAtMost(arr, target) + expect(result).toBe(0) + }) +}) diff --git a/Sliding-Windows/test/MaxSumSubarrayFixed.test.js b/Sliding-Windows/test/MaxSumSubarrayFixed.test.js new file mode 100644 index 0000000000..d4ca107c65 --- /dev/null +++ b/Sliding-Windows/test/MaxSumSubarrayFixed.test.js @@ -0,0 +1,22 @@ +import { maxSumSubarrayFixed } from '../MaxSumSubarrayFixed' + +describe('Fixed-size Sliding Window - maxSumSubarrayFixed', () => { + it('should return the maximum sum of a subarray of size k', () => { + const arr = [2, 1, 5, 1, 3, 2] + const k = 3 + const result = maxSumSubarrayFixed(arr, k) + expect(result).toBe(9) + }) + + it('should throw a RangeError if k is larger than the array length', () => { + const arr = [2, 1, 5] + const k = 4 + expect(() => maxSumSubarrayFixed(arr, k)).toThrow(RangeError) + }) + + it('should throw a RangeError if k is less than 1', () => { + const arr = [2, 1, 5] + const k = 0 + expect(() => maxSumSubarrayFixed(arr, k)).toThrow(RangeError) + }) +}) From a3c5ac035684dab66124b53961d6cf3e44e040a5 Mon Sep 17 00:00:00 2001 From: k7 Date: Tue, 5 Nov 2024 22:34:25 +0530 Subject: [PATCH 2/2] update directory file for sliding windows --- DIRECTORY.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 7f6484cae5..a29ece310e 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -322,6 +322,9 @@ * [StringSearch](Search/StringSearch.js) * [TernarySearch](Search/TernarySearch.js) * [UnionFind](Search/UnionFind.js) +* **Sliding-Windows** + * [MaxSumSubarrayFixed](Sliding-Windows/MaxSumSubarrayFixed.js) + * [LongestSubarrayWithSumAtMost](Sliding-Windows/LongestSubarrayWithSumAtMost.js) * **Sorts** * [AlphaNumericalSort](Sorts/AlphaNumericalSort.js) * [BeadSort](Sorts/BeadSort.js) 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