1. Brief Introduction
Suppose there is a queue with three operations:
1. EnQueue (v): Add v to the queue
2. DeQueue: deletes the first element in the queue and returns this element.
3. MaxElement: returns the maximum element in the queue.
Design a Data Structure and algorithm to minimize the time complexity of MaxElement operations.
2. Ideas
At first, I thought it was the same as the previous custom stack-pop-push-min-time complexity as O (1). Later I found that it was not the same thing, but it was different. For the stack, the inbound and outbound stacks that we can provide do not affect the situation in the secondary array. Assume that there are N elements currently (for simplicity, the subscript starts from 1 ), F [1] of the auxiliary space records the most value position in A [], and F [2] records the most value position in ,···, F [N] records the most value position in A [1, N. Insert F [k + 1] = A [F [k] and A [k + 1] at the most efficient position. The insertion complexity is O (1 ), when deleting node k + 1, delete A [k + 1] and F [k + 1], which does not affect F [1]-F [k], therefore, the complexity of deletion is O (1), and the maximum value is obtained. Assume that N elements are present, and A [F [N] is returned.
For the queue, if the auxiliary array method of the stack is applied, assuming that there are N elements and the subscript starts from 1, F [1] records A [1, the most value in N]. F [2] records the most value in A [2, N]. ·, F [N] records A [N, n. When A [k + 1] enters the queue, update F [1] to F [k], F [K + 1] = K + 1, therefore, the insertion complexity is O (N). When F [k] leaves the queue, you can delete F [k] (the first element is displayed every time you leave the queue, in fact, F [1]-F [k-1] has been out of the queue), so the complexity of deletion is O (1 ). The complexity of getting the most value is O (1 ).
The difference between queues and stacks is clear. The beauty of programming gives two answers: one is to build the largest heap, and the other is to use two stacks for implementation.
Maximum heap method:
The queue itself is either ordered or linked. In addition, for each element in the queue to build a node (including the location in the queue), these nodes constitute a maximum heap, so the insert and delete operations must maintain this maximum heap, the time complexity is O (LogN), and the maximum complexity is O (1 ).
Two stack methods:
Stack A and stack B, both of which are the implementation of the complexity of pop-push-min mentioned earlier.
Take the most value: return the most value of stack A compared with the most value of stack B. Complexity O (1 ).
Join Operation: directly go to stack B. Complexity O (1 ).
Team-out operation: if Stack A is not empty, stack A goes out of Stack directly. The complexity is O (1). If stack A is empty, then stack B is pushed out one by one and one to stack A, and then stack A goes out of stack. complexity O (N) is actually the length of stack B.
For this method, if a series of inbound stacks and a series of outbound stacks are performed during column operations, the stack is first continuously pushed to B and then the first outbound stack, stack B elements are all pushed into stack A, and stack A is one. This step is N's complexity, but since then it is constantly coming out of stack A, which is the complexity of O (1. Not bad. With the help of stack code, it is easy to implement. For such a scenario, only the first time the stack is output, it requires O (N), and the complexity is not very even. For each element, either stack B or stack A will pop up from Stack A, that is, the total number is 3N, and the average value is basically O (3 ), otherwise, the maximum heap O (LogN) is much faster.
3. Reference
The beauty of programming, 3.7, the maximum value in the queue Operation Problems