-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Adding a new algorithm: Sliding Window. #672
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* Sliding Window: | ||
* This pattern involve creating a window which can either be | ||
* an array or numbers from one position to another. | ||
* | ||
* Depending on a certain condition, the window either increases | ||
* or closes (and a new window is created). | ||
* | ||
* Very useful for keeping track of a subset of data in an | ||
* array/string etc. | ||
* | ||
* Time Complexity: Best - O(n); | ||
* | ||
* Examples: | ||
* maxSubarraySum([1,2,5,2,8,1,5],2) // returns 10 | ||
* maxSubarraySum([1,2,5,2,8,1,5],15) // returns null | ||
* maxSubarraySum([5,2,6,9],3) // returns 17 | ||
* @param {[Int]} arr - An array of integers on which we will perform the test. | ||
* @param {Int} num - An integer that displays the size of the window you want to check. | ||
* @returns {Int / Null} - Returns a total of N consecutive numbers or null | ||
*/ | ||
|
||
function slidingWindow(arr, num) { | ||
// Edge Case: | ||
// If the length of the array shorter than the window size (num) return null. | ||
if (arr.length < num) return null; | ||
// The highest amount of consecutive numbers | ||
let maxSum = 0; | ||
// Temp amount of consecutive numbers - For comparative purposes | ||
let tempSum = 0; | ||
// loop over the array {num} times and save their total amount in {maxSum} | ||
for (let i = 0; i < num; i++) { | ||
maxSum += arr[i]; | ||
} | ||
// initialize {tempSum} to {maxSum}. | ||
tempSum = maxSum; | ||
// loop over the array n times | ||
for (let i = num; i < arr.length; i++) { | ||
// Add the next num in the array and remove the first one | ||
tempSum = tempSum - arr[i - num] + arr[i]; | ||
// save the largest number between {maxNum} and {tempNum} in maxSum. | ||
maxSum = Math.max(maxSum, tempSum); | ||
} | ||
return maxSum; | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.