Saturday, March 12, 2011
Special queue
Implement a queue in which push_rear(), pop_front() and get_min() are all O(1) operations.
Subscribe to:
Post Comments (Atom)
Random commentary about Machine Learning, BigData, Spark, Deep Learning, C++, STL, Boost, Perl, Python, Algorithms, Problem Solving and Web Search
8 comments:
Okay, what's the answer to this one? Implement as a heap and you can't do all three simultaneously. What's the answer?
Reply Deleteduplicate items in the queue!
Reply DeleteWell, I thought of running two queues, one a priority queue, the other a normal queue, but that still doesn't get you there. Hmm... I'm stumped. Anyone else get this one?
Reply DeleteBut pop_front has to return elements in FIFO order relative to push_rear or the same element of get_min?
Reply DeleteHow many different values can you have in the queue?
Reply DeleteIf the value space is little you can use an array of queues for each possible value, to the priority problem (get_min). And link every entry in FIFO order too, to push_rear() and pop_front() in O(1).
Reply DeleteOr an hashtable where the key is the value on which apply the sort and the value a list of all entry with the same value.
Reply DeleteThen the structure has to mantains also the FIFO order linking the entries with a linked list.
Not sure that works, Dashie. The value space would have to be very small to do the former solution. And how does the hashtable solution deal with deletes? The problem is that you potentially need to do a linear scan of the queue to find the minimum again in some situations, don't you?
Reply Delete