title: Given an array and the size of a sliding form, find the maximum value in all sliding forms.
Examples Show
For example, suppose the input array {2,3,4,2,6,2,5,1} and the size of the sliding form. Then there are altogether 6 sliding forms, with their maximum values {4,4,6,6,6,5}.
Thinking of solving problems
Assuming brute force method, the problem seems to be easy to solve: the ability to scan all the numbers of each sliding form and find the maximum value. Assuming that the size of the sliding form is K, the O (k) Time talent is needed to find the maximum value in the sliding form. For an input array of length n, the total time complexity of the algorithm is O (NK).
Actually a sliding form can be seen as a queue. When the form slides, the first number in the form is deleted, and at the same time a new number is added to the end of the form.
This conforms to the FIFO feature of the queue.
If we can find out the maximum number of it from the queue, this problem will be overcome.
In the interview question 21. We implemented a stack that was able to get the minimum value with O (1) time. , the maximum value of the stack can be obtained with O (1) time. At the same time in interview question 7, we discussed how to implement a queue with two stacks.
Combining the solutions of these two problems, we find that the assumption is that the queue is implemented with two stacks because the maximum value in the stack can be obtained with O (1) time. The maximum value of the queue can then be obtained with O (1) time, so the total time complexity is reduced to O (n).
We can solve this problem in this way.
Just this is equivalent to a round of interview time to do two face test, time may not be enough. Let's see if there's any other way.
Here's a different idea. Instead of putting each value of a sliding form into a queue, we simply deposit the value that is likely to be the maximum value of a sliding form into a queue of open ends. Next, take the input number {2,3,4,2,6,2,5,1} as an example step analysis.
The first number of the array is 2, put it in the queue. The second number is 3. Because it is larger than the previous number 2, 2 cannot be the maximum value in a sliding form.
2 first delete from the queue, and then put 3 into the queue. At this point there is only one number in the queue 3. The steps for the third number 4 are similar, and finally there is only a number 4 in the queue. There are already 3 numbers in the sliding form. and its maximum value is 4 bits to the head of the queue.
Next, the fourth digit 2 is processed. 2 is smaller than the number 4 in the queue. When 4 slides out of the form 2 is still likely to be the maximum value of the sliding form, so put 2 in the tail of the queue.
Today there are two numbers in the queue, 4 and 2, and the maximum value of 4 is still in the head of the queue.
The next number is 6. Because it is larger than the number 4 and 2 already in the queue, then 4 and 2 are unlikely to be the maximum values in the sliding form. Remove 4 and 2 from the queue first. Then put the number 6 in the queue. At this time the maximum value of 6 is still in the head of the queue.
The sixth number is 2 because it is smaller than the number 6 already in the queue, so 2 is also placed in the tail of the queue. There are two numbers in the queue, with a maximum of 6 in the head of the queue.
The next number is 5. In the queue there are two numbers in 6 and 2. 2 is less than 5. So 2 cannot be the maximum value of a sliding form that can be removed from the tail of the queue. After the number 2 is deleted, the number 5 is saved to the queue. At this point there are two digits in the queue, 6 and 5, where the queue head is the maximum value of 6.
The last digit of the array is 1. Put 1 in the tail of the queue. Note that the number 6 at the head of the queue is the 5th number in the array, and the sliding form does not already contain the number, so the number 6 should be removed from the queue. So how do you know if a sliding form contains a number? The subscript in the array should be stored in the queue, not the number. When the subscript of a number is greater than or equal to the size of the number that is currently being processed. This number has been slid out of the sliding form and can be removed from the queue.
Code Implementation
Import java.util.*; Public classTest65 {Private StaticList<integer>maxinwindows(List<integer> data,intSize) {list<integer> Windowmax =NewLinkedlist<> ();//Condition Check if(Data = =NULL|| Size <1|| Data.size () <1) {returnWindowmax; } deque<integer> idx =NewLinkedlist<> ();//The form is not yet filled. Index of the maximum value to find for(inti =0; I < size && I < data.size (); i++) {//Assuming that the corresponding value of the index is greater or equal than the value of the previously stored index value, delete the previously stored value while(!idx.isempty () && data.Get(i) >= data.Get(Idx.getlast ())) {idx.removelast (); }//Join indexIdx.addlast (i); }//The form is already filled for(inti = size; I < data.size (); i++) {//The maximum value of the first form is savedWindowmax.add (data.Get(Idx.getfirst ()));//Assume that the corresponding value of the index is greater or equal than the value corresponding to the previously stored index value. Delete the previously stored value while(!idx.isempty () && data.Get(i) >= data.Get(Idx.getlast ())) {idx.removelast (); }//delete the corresponding subscript for data that has been slid out of the form if(!idx.isempty () && Idx.getfirst () <= (i-size)) {Idx.removefirst (); }//The largest possible subscript entry teamIdx.addlast (i); }//Last form Max queueWindowmax.add (data.Get(Idx.getfirst ()));returnWindowmax; }Private StaticList<integer>arraytocollection(int[] array) {list<integer> result =NewLinkedlist<> ();if(Array! =NULL) { for(intI:array) {result.add (i); } }returnResult } Public Static void Main(string[] args) {//expected {7};List<integer> data1 = arraytocollection (New int[]{1,3, -1, -3,5,3,6,7}); System. out. println (Data1 +","+ Maxinwindows (data1,Ten));//Expected {3, 3, 5, 5, 6, 7};list<integer> data2 = arraytocollection (New int[]{1,3, -1, -3,5,3,6,7}); System. out. println (Data2 +","+ Maxinwindows (data2,3));//Expected {7, 9, one, a, A.};list<integer> data3 = arraytocollection (New int[]{1,3,5,7,9, One, -, the}); System. out. println (Data3 +","+ Maxinwindows (data3,4));//Expected { +, +, +};list<integer> Data5 = arraytocollection (New int[]{ -, -, A,Ten,8,6,4}); System. out. println (Data5 +","+ Maxinwindows (DATA5,5));//Expected {Ten , +, +, one};list<integer> data6 = arraytocollection (New int[]{Ten, -, A, One}); System. out. println (Data6 +","+ Maxinwindows (DATA6,1));//expected {+};list<integer> data7 = arraytocollection (New int[]{Ten, -, A, One}); System. out. println (Data7 +","+ Maxinwindows (DATA7,4)); }}
Execution Results
Special Instructions
Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/DERRANTCM/article/details/46872411"
"Sword refers to offer study" "Face question 65: Sliding the maximum value of the form"