Problem: Design a Data Structure a SpecialQueue which supports following operations enque, deque, getMin() or getMax() where getMin() operation takes O(1) time.
Let the data to be inserted in queue be - 4, 2, 1, 6 Operation Queue Output push(4) 4 - push(2) 4, 2 - push(1) 4, 2, 1 - getMin() 4, 2, 1 1 push(6) 4, 2, 1, 6 - pop() 2, 1, 6 4 pop() 1, 6 2 pop() 6 1 getMin() 6 6 // Notice the getMin() function call // It returns the minimum element // of all the values present in the queue
Approach: The idea is to use Doubly ended Queue to store in increasing order if the structure is to return the minimum element and store in decreasing order if the structure is to return the maximum element. The operations of the Data Struture is defined as follows:
- Insert the element into the queue structure.
- If the size of the Deque structure is empty that is the size of the Deque is 0. Then, Insert the element from the back.
- Otherwise, If there are some elements in the Deque structure then pop the elements out from the Deque until the back of the Deque is greater than the current element and then finally insert the element from back.
- If the first element of the Deque is equal to the front element of the queue then pop the elements out from the Queue and the Deque at the same time.
- Otherwise, Pop the element from the front of the queue to maintain the order of the elements.
Return the front element of the Deque to get the minimum element of the current element of the queue.
Below is the implementation of the above approach:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Design a data structure that supports insert, delete, search and getRandom in constant time
- Applications of Queue Data Structure
- Design an efficient data structure for given operations
- Design a data structure that supports insert, delete, getRandom in O(1) with duplicates
- Design a stack to retrieve original elements and return the minimum element in O(1) time and O(1) space
- Static Data Structure vs Dynamic Data Structure
- Difference between Stack and Queue Data Structures
- Gap Buffer Data Structure
- Advantages of Trie Data Structure
- Introduction to the Probabilistic Data Structure
- Applications of Graph Data Structure
- Tango Tree Data Structure
- Data Structure for Dictionary and Spell Checker?
- Trie Data Structure using smart pointer and OOP in C++
- Ropes Data Structure (Fast String Concatenation)
- Structure Member Alignment, Padding and Data Packing
- Commonly Asked Data Structure Interview Questions | Set 1
- Inversion Count using Policy Based Data Structure
- Dynamic Disjoint Set Data Structure for large range values
- Maximum and minimum of an array using minimum number of comparisons
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.