Created
May 23, 2021 01:58
-
-
Save misterpoloy/26b69e71693287a50062f64235d1b27b to your computer and use it in GitHub Desktop.
Slidding window pattern
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
function maxSubarraySum(arr, num) { | |
let maxSum = 0; | |
let tempSum = 0; | |
if (arr.length < num) { return null } | |
for (let i = 0; i < num; i++) { | |
maxSum += arr[i] | |
} | |
tempSum = maxSum; | |
for (let i = num; i < arr.length; i++) { | |
tempSum = tempSum - arrp[i - num] + arr[i]; | |
amxSum = Math.max(maxSum, tempSum) | |
} | |
return maxSUM; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment