How can I retrieve the max and min element from a queue at any time in 0(1) time complexity? Earlier I was using Collections.max and min to find the elements but that would be 0(n).
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
I am posting the complete code here to find MIN and MAX in queue in a constant time. Please feel free to contact me if you have any doubt.
Queue
Deque
FindMinMaxQueue
MyQueue
MinAndMaxFinderQueue
Test
I suspect you are trying to implement what a PriorityQueue does. This is a sorted queue which O(log N) to get the lowest value. I not sure why you would want to largest value as a queue only has one end.
I would store two fields minIndex and maxIndex that will store index positions in your data structure for the minimum and maximum value respectively.
When new elements are added to the queue, check for two things:
This will give you a O(1) asymptote for the current min and max value.
You only have 2 ways to get O(1) for a min/max operation:
There exist such a structure that acts like a queue but lets you retrieve min/max value in constant time, actually not strictly constant, it is amortized constant time (named min/max queue as you could guess). There are two ways of implementing it - using two stacks or using a queue and a deque.
Deque implementation looks more less like this (language agnostic):
so we have a deque of max elements, the one on the front is the desired max, and a standard queue.
Push operation
Remove operation
Get max
(lots of arguments should be added to make it clear why it works, but the second version presented below may be the answer to this necessity)
The Stack implementation is quite similar, I think it may be a bit longer to implement but perhaps easier to grasp. The first thing to note is that it is easy to store the maximal element at the stack - easy exercise (for the lazy ones - Stack with find-min/find-max more efficient than O(n)?). The second part, perhaps a bit tricky if seen the first time, is that it is quite easy to implement a queue using two stacks, it can be found here - How to implement a queue using two stacks? . And that is basically it - if we can get the maximal element of both of the stacks we can get the maximal element of the whole queue (taking maximum is associative or something like that if you want a more formal argument, but I bet you don't, it is really obvious).
The min versions is done analogically.
Everything may also be done using a set (or something of it's kind) in O(nlogn) time but it is pointless as the constant in O(n) is really small and it should be much faster, yet easy to implement.
NON-INTERESTING parts from the first version:
Hope I helped a little bit. And hope that didn't say anything wrong. Can give a simple implementation in C++/C if required. Would be grateful for any feedback on the form as it is my first post of this type anywhere :) (and English is not my native language). Also some confirmation on the correctness would be great.
EDIT: as this answer got me some points I felt obliged to clean it up a bit, also extending it a bit.
Implement a queue in which push_rear(), pop_front() and get_min() are all constant time operations