Talking about monotonous queue, monotonous stack, and queue
When talking about this topic, I believe many people may feel enlightened but uncertain. Yes, this is because the word "monotonic" exists. What is monotonic? people who have learned functions know the monotonicity of monotonic functions or functions, to put it bluntly, monotonic means increasing or decreasing all the time. For example, 1, 3, 5, and 9 are a monotonically increasing sequence. The latter number is smaller than the former. The topics mentioned here also have similar characteristics.
Let's talk about monotonous queues first! A monotonous queue is a queue that conforms to the monotonic nature. It also has a monotonous nature and a queue nature. He is not frequently used in programming, but plays a crucial role. Its function is very simple. It is to maintain a group of monotonous data, so that we can quickly seek the maximum or minimum values of the first k or the last k in the running process. As for the monotonous stack, I believe there will be a rough understanding after reading the above description. The monotonous stack is a monotonous stack with a monotonous nature and a stack. In terms of function, the two are the same, and the difference is only that the array maintained in the programming process is different.
Next I will give a simple column to explain monotonous queues and monotonous stacks.
Example: there is a set of data, such as, and 6. They will follow this input. At the same time, you will be asked to find the maximum value of the next n number at a certain time point.
Based on the meaning of the question, we can draw a conclusion that if the last number is greater than the previous number, the result will not be the previous number (for example, because 1 <5, therefore, no matter the maximum values in the last few values are not 1). Therefore, we only need to maintain a monotonically decreasing array to quickly obtain the required values. (Array changes are as follows: Input -- 1, array -- 1; input -- 5, because 5> 1 Delete 1 add 5, array -- 5; input -- 9, because 9> 5 delete 5 add 9, array -- 9; input -- 4, because 4 <9 Add directly, array -- 9, 4; input -- 7, because 7> 4 at the same time 7 <9, delete 4 to add 7, array -- 9, 7; input -- 8, because 8> 4 at the same time 8 <9 so Delete 7 to add 8, array -- 9, 8; input -- 6, because 6 <8 is directly added to the array -- 9, 8, 6 .)
This is the basis of monotonous queues and monotonous stacks, and the rest is only for personal understanding and practice. We recommend a few questions, 1012 and 1047 in the big field of vision, of which 1012 are relatively bare and suitable for beginners. 1047 is slightly difficult. We recommend that you do it after 1012. (Here we will give you a prompt that 1047 uses two monotonous queues and stacks, and uses the result at the same time .)
Finally, thank you for reading my blog. I hope it will help you.