Fundamentals/Sliding Window Introduction
← PrevNext →
Given an array nums of non-negative integers and an integer k, return the largest sum among all contiguous subarrays of length k.
Input
nums: array of non-negative integers
k: window size
Output
The maximum sum of any length-k contiguous subarray.
Examples
Example 1
Inputnums = [1, 2, 3, 4, 5], k = 3
Output12
Explanation: The window [3, 4, 5] sums to 12.
Example 2
Inputnums = [10, 20], k = 2
Output30
Constraints
1 <= k <= nums.length
Maintain a running window sum; slide by adding the new right element and removing the old left element.