Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3f52add

Browse files
Implement sliding window algorithms for fixed and dynamic sizes with tests (TheAlgorithms#1765)
* feat: implement sliding window algorithms for fixed and dynamic sizes with tests * update directory file for sliding windows
1 parent 1d252d7 commit 3f52add

File tree

5 files changed

+96
-0
lines changed

5 files changed

+96
-0
lines changed

‎DIRECTORY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,9 @@
321321
* [StringSearch](Search/StringSearch.js)
322322
* [TernarySearch](Search/TernarySearch.js)
323323
* [UnionFind](Search/UnionFind.js)
324+
* **Sliding-Windows**
325+
* [MaxSumSubarrayFixed](Sliding-Windows/MaxSumSubarrayFixed.js)
326+
* [LongestSubarrayWithSumAtMost](Sliding-Windows/LongestSubarrayWithSumAtMost.js)
324327
* **Sorts**
325328
* [AlphaNumericalSort](Sorts/AlphaNumericalSort.js)
326329
* [BeadSort](Sorts/BeadSort.js)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Function to find the longest subarray with a sum <= target.
3+
*
4+
* @param {number[]} arr - The input array of numbers.
5+
* @param {number} target - The target sum for the dynamic window.
6+
* @returns {number} - The length of the longest subarray with a sum <= target.
7+
*/
8+
export function longestSubarrayWithSumAtMost(arr, target) {
9+
let maxLength = 0
10+
let windowSum = 0
11+
let left = 0
12+
for (let right = 0; right < arr.length; right++) {
13+
windowSum += arr[right]
14+
while (windowSum > target) {
15+
windowSum -= arr[left]
16+
left++
17+
}
18+
maxLength = Math.max(maxLength, right - left + 1)
19+
}
20+
return maxLength
21+
}

‎Sliding-Windows/MaxSumSubarrayFixed.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Function to find the maximum sum of a subarray of fixed size k.
3+
*
4+
* @param {number[]} arr - The input array of numbers.
5+
* @param {number} k - The fixed size of the subarray.
6+
* @returns {number} - The maximum sum of any subarray of size k.
7+
* @throws {RangeError} - If k is larger than the array length or less than 1.
8+
*/
9+
export function maxSumSubarrayFixed(arr, k) {
10+
if (k > arr.length || k < 1) {
11+
throw new RangeError(
12+
'Subarray size k must be between 1 and the length of the array'
13+
)
14+
}
15+
let maxSum = 0
16+
let windowSum = 0
17+
for (let i = 0; i < k; i++) {
18+
windowSum += arr[i]
19+
}
20+
maxSum = windowSum
21+
for (let i = k; i < arr.length; i++) {
22+
windowSum += arr[i] - arr[i - k]
23+
maxSum = Math.max(maxSum, windowSum)
24+
}
25+
return maxSum
26+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { longestSubarrayWithSumAtMost } from '../LongestSubarrayWithSumAtMost'
2+
3+
describe('Dynamic-size Sliding Window - longestSubarrayWithSumAtMost', () => {
4+
it('should return the longest subarray length with sum <= target', () => {
5+
const arr = [1, 2, 3, 4, 5]
6+
const target = 7
7+
const result = longestSubarrayWithSumAtMost(arr, target)
8+
expect(result).toBe(3)
9+
})
10+
11+
it('should return the full array length if the entire sum is within the target', () => {
12+
const arr = [1, 1, 1, 1]
13+
const target = 4
14+
const result = longestSubarrayWithSumAtMost(arr, target)
15+
expect(result).toBe(4)
16+
})
17+
18+
it('should return 0 if no subarray meets the sum condition', () => {
19+
const arr = [5, 6, 7]
20+
const target = 3
21+
const result = longestSubarrayWithSumAtMost(arr, target)
22+
expect(result).toBe(0)
23+
})
24+
})
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { maxSumSubarrayFixed } from '../MaxSumSubarrayFixed'
2+
3+
describe('Fixed-size Sliding Window - maxSumSubarrayFixed', () => {
4+
it('should return the maximum sum of a subarray of size k', () => {
5+
const arr = [2, 1, 5, 1, 3, 2]
6+
const k = 3
7+
const result = maxSumSubarrayFixed(arr, k)
8+
expect(result).toBe(9)
9+
})
10+
11+
it('should throw a RangeError if k is larger than the array length', () => {
12+
const arr = [2, 1, 5]
13+
const k = 4
14+
expect(() => maxSumSubarrayFixed(arr, k)).toThrow(RangeError)
15+
})
16+
17+
it('should throw a RangeError if k is less than 1', () => {
18+
const arr = [2, 1, 5]
19+
const k = 0
20+
expect(() => maxSumSubarrayFixed(arr, k)).toThrow(RangeError)
21+
})
22+
})

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /