Answered step by step
Verified Expert Solution
Question
1 Approved Answer
IN PYTHON PROBLEM STATEMENT: In today s Lab we will explore ways to design a Queue with O ( 1 ) lookup time of the
IN PYTHON
PROBLEM STATEMENT:
In todays Lab we will explore ways to design a Queue with O lookup time of the Maximum
element.
You will solve the problem as stated below:
Here you will Maintain two Queues a Main Queue and a Queue holding the Maximum values
from the Main Queue AKA Max Queue The Main Queue contains the elements. The Max
Queue contains the elements with Maximum value. The Max Queue would have to be a double
ended Queue as you would like to be able to remove elements from both ends.
Example:
Lets say we have the following:
We add an integer into our Main Queue and I hope it is really obvious that when the Main
Queue contains a single element, the Max Queue can be popu lated without confusion :
Main Queue: front of Queue
Max Queue : front of Queue
Now, lets say we insert a into the Main Queue. the Main Queue will look as follows:
Main Queue: front of Queue
In the Max Queue, we dont need anymore, since can never be the Max of this Queue now.
So we remove and insert
Main Queue: front of Queue
Max Queue: front of Queue
Say we insert into the Main Queue. We know is not the Max, but it can be the Max if we de
Queue and from the Queue. So we insert it onto the Max Queue:
MainQueue:
This is because elements could never be Max after is inserted. What I stated above is
exactly the algorithm for inserting an element in the Max Queue.
To lookup the Maximum Value AKA Max we just check the front of the Max Queue which
ensures O lookup time.
While dequeuing elements, we check if they are equal to the front of the Max Queue,and if so
we deQueue from the Max Queue too. For example, after dequeuing lets say we want to de
Queue We see that is the front of the
Max Queue, so we remove both the s This does indeed make sense as can no longer remain
the Maximum after it is removed from the Main Queue.
If the process described above is followed and you code up the example provided we end up with
the complexity stated below.
Time Complexity of the Max Queue lookup: O
Space Complexity on the Max Queue : On
Submissions that dont meet the mentioned Time and Space complexities will have credit
taken off
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started