Sliding window sum
Your task
Given an integer array nums and a window size k (1 <= k <= length of nums), return a list containing the sum of every window of k consecutive elements, from the leftmost window to the rightmost, n - k + 1 sums in total. Compute each subsequent sum from the previous one in O(1) rather than re-adding k elements. Example: nums = [1, 2, 3, 4], k = 2 gives [3, 5, 7].
Examples
window_sums(nums: list[int], k: int) -> list[int]Example 1
Input
window_sums([1, 2, 3, 4], 2)Output
[3, 5, 7]