Movatterモバイル変換


[0]ホーム

URL:


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

Implement sliding window algorithms for fixed and dynamic sizes with tests#1765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
Kesavan-77 wants to merge2 commits intoTheAlgorithms:master
base:master
Choose a base branch
Loading
fromKesavan-77:master
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletionsDIRECTORY.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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)
Expand Down
21 changes: 21 additions & 0 deletionsSliding-Windows/LongestSubarrayWithSumAtMost.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -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
}
26 changes: 26 additions & 0 deletionsSliding-Windows/MaxSumSubarrayFixed.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -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
}
24 changes: 24 additions & 0 deletionsSliding-Windows/test/LongestSubarrayWithSumAtMost.test.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -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)
})
})
22 changes: 22 additions & 0 deletionsSliding-Windows/test/MaxSumSubarrayFixed.test.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -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)
})
})

[8]ページ先頭

©2009-2025 Movatter.jp