1.7 Generate an array of window maximums
2021-04-02 15:26
标签:pop run selection searching case mos slide [] from 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 is slid to the right one position. For example, when the array is [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 are 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: time complexity:O(n * w) Teacher Zuo: 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 标签:pop run selection searching case mos slide [] from 原文地址:https://www.cnblogs.com/latup/p/9216921.html 1 //Generate an array of window maximums
2 void getMaxWindow(int arr[], int n, int res[], int winsize)
3 {
4 int i, 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.
6 j = 0;
7 k = 0;
8 while (k 1) //When the array arr is not traversed, continue searching for the subsequent maximum window.
9 {
10 max = 0;
11 for (i = 0; i //Find the maximum value in the current window.
12 {
13 if (max arr[j])
14 {
15 max = arr[j];
16 }
17 }
18 res[k++] = max; //Record the maximum value of the current window.
19 j = j - winsize + 1; //Slide the window to the right one position.
20 }
21 }
1 //Generate an array of window maximums
2 int* getMaxWindow(const int arr[], int n, int w)
3 {
4 if (arr == NULL || w 1 || n w)
5 {
6 return NULL;
7 }
8 dequeint> qmax; //Deque, storing subscripts of qualifying array elements
9 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 run
10 int index = 0;
11 for (int i = 0; i //i used to track arr array elements
12 {
13 while (!qmax.empty() && arr[qmax.back()] //When the deque is not empty, and the array element corresponding to the tail element is not greater than arr[i], the element at the end of the queue is popped up.
14 {
15 qmax.pop_back();
16 }
17 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 is enqueued.
18 if (qmax.front() == i - w) //If the element of the head (ie the array index) is not in the current window, the element is dequeued.
19 {
20 qmax.pop_front();
21 }
22 if (i >= w - 1) //When the traversed element is sufficient for a window size, the largest arr array element in the selection window is stored in the array res.
23 {
24 res[index++] = arr[qmax.front()];
25 }
26 }
27 return res; //return the array of res
28
29 }
文章标题:1.7 Generate an array of window maximums
文章链接:http://soscw.com/index.php/essay/71435.html