FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScript/Search/SlidingWindow.js at master · TheAlgorithms/JavaScript · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
TheAlgorithms
/
JavaScript
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
5.8k
Star
34.2k
Code
Issues
21
Pull requests
192
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScript
/
Search
/
SlidingWindow.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
47 lines (45 loc) · 1.6 KB
Breadcrumbs
JavaScript
/
Search
/
SlidingWindow.js
Copy path
File metadata and controls
47 lines (45 loc) · 1.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* 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
}
export
{
slidingWindow
}
Back
|
FazBrowse Home
|
New Git URL