Article Directory
topic description
【Sliding window maximum value】
There is an array of N integers and a window with a length of M. The window slides from the first number in the array until the window cannot slide.
Each window sliding generates a window sum (the sum of all numbers in the window), and finds the maximum value of all window sums generated by window sliding.
Enter a description:
The first line enters a positive integer N, indicating the number of integers. (0<N<100000)
Enter N integers in the second line, and the range of integers is [-100,100].
The third line enters a positive integer M, M represents the size of the window, M<=100000, and M<=N.
Output description:
Window sliding yields the maximum of all window sums.
train of thought
To solve this problem, we can use a sliding window algorithm to calculate the window sum. Specific steps are as follows:
- Enter N, array elements, and M to store the array elements in a list.
- The initial position left and the end position right of the initialization window are 0, and the sum of the current window is 0.
- Slide the right to the right while accumulating elements into sum until the window