Last active
October 14, 2015 00:58
-
-
Save richzw/4282952 to your computer and use it in GitHub Desktop.
monotonous queue implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 const int LEN = 3; | |
9 struct node{ | |
10 int val; | |
11 int seq; | |
12 }monoto_Q[LEN]; | |
13 | |
14 void findAscend_MonotonousQueue(int arr[], int len, int qSize){ | |
15 if (arr == NULL || len <= LEN) | |
16 return; | |
17 int index = 0, tail = 0, head = 1; | |
18 | |
19 //initialize the monotonous queue | |
20 for (index = 0; index < qSize; ++index){ | |
21 while (head <= tail && arr[index] > monoto_Q[tail].val){ | |
22 --tail; | |
23 } | |
24 monoto_Q[++tail].val = arr[index]; | |
25 monoto_Q[tail].seq = index; | |
26 } | |
27 | |
28 //handle the data array | |
29 for (index = qSize - 1; index < len; ++index){ | |
30 while (head <= tail && arr[index] > monoto_Q[tail - 1].val){ | |
31 --tail; | |
32 } | |
33 monoto_Q[++tail].val = arr[index]; | |
34 monoto_Q[tail].seq = index; | |
35 while (index - monoto_Q[head].seq > qSize - 1) | |
36 ++head; | |
37 cout << monoto_Q[head].val << " "; | |
38 } | |
39 cout << endl; | |
40 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment