Design a Queue with O(1) lookup time of the Maximum element. You will implement this design using
Fantastic news! We've Found the answer you've been seeking!
Question:
Design a Queue with O(1) lookup time of the Maximum element. You will implement this design using the Array Deque Class in Python.
You will Maintain two Queues - a Main Queue and a Queue holding the Maximum value(s) 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
Transcribed Image Text:
Example: Let's say we have the following: We add an integer 1 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: 1 << front of Queue Max Queue: 1 << front of Queue Now, let's say we insert a 4 into the Main Queue. the Main Queue will look as follows: Main Queue: 4 → 1 << front of Queue In the Max Queue, we don't need 1 anymore, since 1 can never be the Max of this Queue now. So we remove 1 and insert 4. Main Queue: 4 → 1 << front of Queue Max Queue: 4 << front of Queue Say we insert 2 into the Main Queue. We know 2 is not the Max, but it can be the Max if we de-Queue 1 and 4 from the Queue. So, we insert it onto the Max Queue: Main Queue: 2→ 4 → 1 << front of Queue Max Queue: 2→ 4 << front of Queue Further, If we insert a 3 into the Main Queue, we can get rid of the 2 from the Max Queue, because 2 can no longer be the Max of the Queue, even if 4 and 1 are de-Queued. In that case our Queues become: Main Queue: 32 4+1 << front of Queue << front of Queue Max Queue: 3→ 4 In the process of inserting 3, we removed elements from the back of the Max Queue until we found an element > 3. This is because elements < 3 could never be Max after 3 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(1) lookup time. Example: Let's say we have the following: We add an integer 1 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: 1 << front of Queue Max Queue: 1 << front of Queue Now, let's say we insert a 4 into the Main Queue. the Main Queue will look as follows: Main Queue: 4 → 1 << front of Queue In the Max Queue, we don't need 1 anymore, since 1 can never be the Max of this Queue now. So we remove 1 and insert 4. Main Queue: 4 → 1 << front of Queue Max Queue: 4 << front of Queue Say we insert 2 into the Main Queue. We know 2 is not the Max, but it can be the Max if we de-Queue 1 and 4 from the Queue. So, we insert it onto the Max Queue: Main Queue: 2→ 4 → 1 << front of Queue Max Queue: 2→ 4 << front of Queue Further, If we insert a 3 into the Main Queue, we can get rid of the 2 from the Max Queue, because 2 can no longer be the Max of the Queue, even if 4 and 1 are de-Queued. In that case our Queues become: Main Queue: 32 4+1 << front of Queue << front of Queue Max Queue: 3→ 4 In the process of inserting 3, we removed elements from the back of the Max Queue until we found an element > 3. This is because elements < 3 could never be Max after 3 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(1) lookup time.
Expert Answer:
Answer rating: 100% (QA)
I can help you design a queue with O1 lookup time of the maximum element using the array deque class in Python Here are the steps that you need to fol... View the full answer
Related Book For
Introduction to Java Programming, Comprehensive Version
ISBN: 978-0133761313
10th Edition
Authors: Y. Daniel Liang
Posted Date:
Students also viewed these operating system questions
-
Explain the difference between visionary leadership and transactional leadership ?
-
Managing Scope Changes Case Study Scope changes on a project can occur regardless of how well the project is planned or executed. Scope changes can be the result of something that was omitted during...
-
Hyten Corporation On June 5, 1998, a meeting was held at Hyten Corporation, between Bill Knapp, Director of Marketing/Sales, and John Rich, director of engineering. The purpose of the meeting was to...
-
(2) Use Figure 10.2 shows a multi-degree-of-freedom (MDOF) model of three connected masses. Find the following for the MDOF system in Figure 10.2: Find expressions for the kinetic energy (T) and...
-
Early UNIX systems used swapping for memory management, whereas 4.3BSD used paging and swapping. Discuss the advantages and disadvantages of the two memory methods.
-
Assume Nikken Microsystems has sold Internet servers to Telecom Espaa for 700,000. Payment is due in 3 months and will be made with a trade acceptance from Telecom Espaa Acceptance. The acceptance...
-
In an Otto cycle, air at \(15^{\circ} \mathrm{C}\) and 1 bar is compressed adiabatically until the pressure is 15 bar. Heat is added at constant volume until the pressure rises to 40 bar. Calculate...
-
Perkins Processors Inc. produces an average of 10,000 units each month. The factory standards are 20,000 hours of direct labor and 10,000 pounds of materials for this volume. The standard cost of...
-
Modern gas turbine engines operate at fuel-to-air ratios of about 0.02, pressure ratios (ratio of compressor exit pressure to inlet pressure) of nearly 40, and with a turbine inlet temperature...
-
Benito Mendez opened Mendez Appraisals. He rented office space and has a part-time secretary to answer the telephone and make appraisal appointments. His chart of accounts is as follows: Mendezs...
-
An object of mass m moving in a viscous medium with resistive force that is proportional to velocity -Rv which satisfies the differential equation below: dv dt where m = 2kg , R = 1.2 Ns/m and...
-
A corporate taxpayer's capital gains and losses are as follows: Short-term capital gain $7,000 Short-term capital loss $(43,000) Long-term capital gain $ 9,000 Long-term capital loss $(21,000) What...
-
An accrual-basis taxpayer sold land for $100,000 on July 1, Year 1, and received $20,000 cash and a note for $80,000. The taxpayer's basis in the asset is $60,000. What amount of gain, if any, should...
-
The logistic function defined by: 1 a + e-t N(t) = t0 represents the growth of a population. We will derive this func- tion by solving a differential equation model sume that a is a positive...
-
If you go on a motorcycle trip of 200 miles in the mountains and 8/19 of the trip is uphill, how many miles of the trip are not uphill? Reduce to the lowest terms
-
Figure 1, shows an RC continuous beam supporting various loads and actions. The loading condition and dimension data are given in Figure 1 and as a function of UC (uniqueness code). Your Uniqueness...
-
Congratulations, you recently inherited a farm, and decided to purchase a new road-safe tractor unit for $98,000! You plan on keeping the tractor unit for the next six years, at which time you will...
-
The liquidliquid extractor in Figure 8.1 operates at 100F and a nominal pressure of 15 psia. For the feed and solvent flows shown, determine the number of equilibrium stages to extract 99.5% of the...
-
The implementations of the methods contains(E e), get(int index), indexOf(E e), lastIndexOf(E e), and set(int index, E e) are omitted in the text. Implement these methods.
-
Listing 2.7, ShowCurrentTime.java, displays the current time. Improve this example to display the current date and time. The calendar example in Listing 6.12, PrintCalendar.java, should give you some...
-
The text implements Prims algorithm using lists for adjacent edges. Implement the algorithm using an adjacency matrix for weighted graphs.
-
Geneva Ltd has a year end of 30 June for its financial statements. You are provided with the following information relating to rent payable: You are required to: calculate the amount that should be...
-
Beijing Ltd runs a small hotel that commenced trading on 1 March. The following transactions occurred during the month of March: 1 March Share capital of 50,000 was invested by the owners who also...
-
The financial year end for Florence Ltd is 31 December 20X7. Calculate the amount charged to the income statement and the balance outstanding (for the balance sheet) in respect of the following...
Study smarter with the SolutionInn App