1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| fun slidingWindow(nums: IntArray, k: Int): Int { val n = nums.size var sum = 0 var maxSum = 0
for (i in 0 until k) { sum += nums[i] }
maxSum = sum
for (i in k until n) { sum += nums[i] - nums[i - k] maxSum = maxOf(maxSum, sum) }
return maxSum }
|