dsa · easy
Max Sum of Subarray of Size K
GreyOrangeArraySliding WindowFoundation
Return the maximum sum of any contiguous subarray of length k.
Arguments
nums— the input array of integersk— window length — every subarray considered has exactlykelements
**Example:** [2,1,5,1,3,2], k=3 → 9 ([5,1,3]).
Constraints
1 <= k <= nums.length <= 10^5 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.
Examples
Example 1
Input: [2,1,5,1,3,2] 3 Expected: 9
Example 2
Input: [2,3,4,1,5] 2 Expected: 7