Leetcode interview question 59 – II. Maximum value of queue

Time:2021-9-27

Interview question 59 – II. Maximum value of queue


Title Source: https://leetcode-cn.com/problems/dui-lie-de-zui-da-zhi-lcof

subject


Please define a queue and implement the function max_ Value gets the maximum value in the queue and requires the function max_ value、push_ Back and pop_ The average sharing time complexity of front is O (1).

If the queue is empty, pop_ Front and Max_ Value needs to return – 1

Example 1:

Input: 
["MaxQueue","push_back","push_back","max_value","pop_front","max_value"]
[[],[1],[2],[],[],[]]
Output: [null, null, null, 2,1,2]

Example 2:

Input: 
["MaxQueue","pop_front","max_value"]
[[],[],[]]
Output: [null, - 1, - 1]

Problem solving ideas


  1. Idea: auxiliary queue
  2. Original queue: record the original elements; Auxiliary queue: process based on the original queue to ensure that the first element is the maximum (when the queue is not empty).
  3. No judgment is made for adding elements to the original queue. When elements are to be added, they can be added directly. It is mainly to maintain the sequence of auxiliary queues and ensure that the first element is the maximum value. The specific idea is as follows: when the queue is not empty and the last element is smaller than the element to be added, pop up the last element until the queue is empty or the last element is larger than the element to be added.
  4. When the original queue pops up the first element, the auxiliary queue should also be adjusted according to the situation. Otherwise, the original queue pop-up element is the same as the first element of the auxiliary queue, but the element still exists in the auxiliary queue, which will lead to an error in getting the maximum value. Solution: when ejecting an element from the original queue, judge whether it is the same as the first element of the auxiliary queue. If it is the same, the auxiliary queue should also eject this element.
  5. Reason for importing deque: because of the common listpop(0)Method pops up the first element. The time complexity is O (n), which does not meet the requirements of the topic, and deque’spopleft()The time complexity of the method is O (1).

List and deque pop up the first element. The time complexity is as follows:

Leetcode interview question 59 - II. Maximum value of queue

Leetcode interview question 59 - II. Maximum value of queue

graphic

code implementation


class MaxQueue:

    def __init__(self):
        #Import deque
        #Because the complexity of Pop (n) of the original list is O (n)
        #Deque's complexity of complex () is O (1)
        from collections import deque
        self.queue = deque()
        self.aux_queue = deque()

    def max_value(self) -> int:
        #When the auxiliary queue is not empty, the first element is returned
        #Otherwise - 1 is returned
        return self.aux_queue[0] if self.aux_queue else -1

    def push_back(self, value: int) -> None:
        #Put the elements into the queue. The original queue does not make judgment, but directly put them into the queue
        self.queue.append(value)
        #Because to keep the auxiliary queue, the first element must be the maximum value
        #Therefore, when the auxiliary queue is not empty and the end element is smaller than the added element, the end element will pop up
        #Until the queue is empty or the end element is larger than the element to be added
        while self.aux_queue and self.aux_queue[-1] < value:
            self.aux_queue.pop()
        #Finally, the elements to be added are added to the auxiliary queue
        self.aux_queue.append(value)

    def pop_front(self) -> int:
        #If the queue is empty, return - 1
        if not self.queue:
            return -1
        #Pop up the first element added
        res = self.queue.popleft()
        #Also note that the auxiliary queue,
        #When the element ejected from the original queue happens to be the first element of the auxiliary queue
        #Here, the auxiliary queue also talks about the element pop-up
        if res == self.aux_queue[0]:
            self.aux_queue.popleft()
        return res
        

# Your MaxQueue object will be instantiated and called as such:
# obj = MaxQueue()
# param_1 = obj.max_value()
# obj.push_back(value)
# param_3 = obj.pop_front()

Realization effect


Leetcode interview question 59 - II. Maximum value of queue


The above is the main content of using auxiliary queue and combining collections.deque library to solve the problem of interview question 59 – II. Maximum value of queue.


Welcome to WeChat official account, book collection.

Recommended Today

Swift advanced (XV) extension

The extension in swift is somewhat similar to the category in OC Extension can beenumeration、structural morphology、class、agreementAdd new features□ you can add methods, calculation attributes, subscripts, (convenient) initializers, nested types, protocols, etc What extensions can’t do:□ original functions cannot be overwritten□ you cannot add storage attributes or add attribute observers to existing attributes□ cannot add parent […]