1.7 Generate an array of window maximums

Posted latup

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1.7 Generate an array of window maximums相关的知识,希望对你有一定的参考价值。

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:

 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 < n - winsize + 1)            //When the array arr is not traversed, continue searching for the subsequent maximum window.
 9     {
10         max = 0;
11         for (i = 0; i < winsize; i++, j++) //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 }

time complexity:O(n * w) 

 

Teacher Zuo:

 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     deque<int> 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 < n; i++)          //i used to track arr array elements
12     {
13         while (!qmax.empty() && arr[qmax.back()] <= arr[i])  //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 }

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的主要内容,如果未能解决你的问题,请参考以下文章

An Array of Sequences

[leetcode]Array-697. Degree of an Array

Degree of an Array

697. Degree of an Array

Equal Sides Of An Array

697. Degree of an Array