Title:
There is an integer array, arr, and a window of size W slides from the leftmost to the rightmost edge of the array. Each time the window was slid to the right one position.
For example, when the array was [4, 3, 5, 4, 3, 3, 6, 7], and the window size is 3:
[4 3 5] 4 3 3 6 7 The maximum value in the window is 5
4 [3 5 4] 3 3 6 7 The maximum value in the window is 5
4 3 [5 4 3] 3 6 7 The maximum value in the window is 5
4 3 5 [4 3 3] 6 7 The maximum value in the window is 4
4 3 5 4 [3 3 6] 7 The maximum value in the window is 6
4 3 5 4 3 [3 6 7] The maximum value in the window is 7
If the array length is n and the window size is W, a total of n-w+1 windows is generated.
Please implement a function:
Input:integer array arr, window size W.
Output:an array Res of length n-w+1, res [i] represents the maximum value of each window state.
In this case, the result should return {5, 5, 5, 4, 6, 7}
Solution:
My:
1 //generate An array of window maximums2 voidGetmaxwindow (intArr[],intNintRes[],intwinsize)3 {4 intI, J, K, Max;//variable I controls sliding in a window, Variable J controls sliding on array arr.5 //the variable k is the index of the array res, the variable max records the maximum value in the current window.6j =0;7K =0;8 while(K < N-winsize +1)//when The array arr is not a traversed, continue searching for the subsequent maximum window.9 {TenMax =0; One for(i =0; i < winsize; i++, J + +)//find The maximum value in the current window. A { - if(Max <Arr[j]) - { theMax =Arr[j]; - } - } -res[k++] = max;//record The maximum value of the current window. +j = j-winsize +1;//slide the window to the right one position. - } +}
Time Complexity:o (n * w)
Teacher Zuo:
1 //generate An array of window maximums2 int* Getmaxwindow (Const intArr[],intNintW)3 {4 if(arr = = NULL | | W <1|| N <W)5 {6 returnNULL;7 }8deque<int> Qmax;//deque, storing subscripts of qualifying array elements9 int* Res =New int[N-w +1];//new can dynamically allocate memory at run time, need to use delete [] res; release memory at the end of the program RunTen intindex =0; One for(inti =0; I < n; i++)//i used to track arr array elements A { - while(!qmax.empty () && arr[qmax.back ()] <= arr[i])//when The deque is isn't empty, and the array element corresponding to the tail element was not greater than arr[i], the Element at the end of the ' queue is ' popped up. - { the Qmax.pop_back (); - } -Qmax.push_back (i);//when The queue is empty or the array element corresponding to the tail element is greater than arr[i], the index i S enqueued. - if(Qmax.front () = = i-w)//if the element of the head (ie the array index) is a not in the current window and the element is dequeued. + { - Qmax.pop_front (); + } A if(I >= W-1)//when The traversed element is sufficient for a window size, the largest arr array element in the selection window I s stored in the array res. at { -res[index++] =Arr[qmax.front ()]; - } - } - returnRes; Return the array of res - in}
Time Complexity:o (n)
This method has lower time complexity and better performance than the first one.
1.7 Generate An array of window maximums