Offer] [prove safety face questions: stacks and queues

Offer prove safety surface in a total of four questions about the stacks and queues

9 is inscribed: two stacks to queue

Description Title: two stack to implement a queue, the completion queue Push and Pop operations. Queue elements int.

For details, see: [surface] to prove safety questions 9 Offer: queue implemented with two stacks --JS

30 is inscribed: a function comprising a stack min

Description Title: stack data structure definition, implement this type can be a min function smallest elements contained in the stack. In this stack, call min, time complexity should push and pop is O (1).

:( idea to establish an auxiliary stack, the minimum recorded after each change in the data stack)

push function: the data value onto the stack; while each of the smallest element (both elements smallest element pushed onto the stack before the new smaller value) are stored in another auxiliary stack (at this time, if the auxiliary stack value is null or smaller than the auxiliary stack top element, this value is then pressed into the auxiliary stack, or pressed into the top element itself to the auxiliary stack).

pop functions: data if the auxiliary stack and the stack is not empty, the top element of the pop up respectively.

min function: if the auxiliary data stack and the stack is not empty, the top element of the auxiliary stack is returned.

31 is inscribed: stack push, pop sequence

Description Title: two input sequence of integers, the first sequence indicating whether a sequence of stack of press-fitting, the second sequence is determined may make the pop-up sequence for the stack. All figures are not pushed onto the stack is assumed equal. Such as a sequence of a sequence of 1,2,3,4,5 is pressed into the stack, the push sequence is 4,5,3,2,1 sequence corresponding to a sequence of pop, but 4,3,5,1,2 it is impossible to push pop-up sequence of the sequence. (Note: the length of the two sequences are equal)

:( idea to establish an auxiliary stack, for example analysis revealed law) if the next number to pop up just the numbers stack, then pop up directly; if the next number not want to eject the stack, the stack has not put onto the stack in the sequence of digital pressed into the auxiliary stack until the next pop required number pressed into the top of the stack so far; the next number after a pop-up if all the numbers are pushed onto the stack is still not found, then the sequence can not be a pop-up sequence.

59 is inscribed: maximum queue

A maximum value of the subject, the sliding window

Description Title: Given an array and the size of the sliding window, sliding window to find the maximum value among all values. For example, if the input array size and {2,3,4,2,6,2,5,1} 3 sliding window, then the presence of a total of six sliding window, their maximum values ​​is {4,4,6, 6,6,5}; it has the following six array {2,3,4,2,6,2,5,1} for the sliding window: {[2,3,4], 2,6,2,5 , 1}, {2, [3,4,2], 6,2,5,1}, {2,3, [4,2,6], 2, 5}, {2,3,4 , [2,6,2], 5,1}, {2,3,4,2, [6,2,5], 1}, {2,3,4,2,6, [2,5, 1]}.

Thinking :( both ends open queue) we use a queue open at both ends, it is possible to save the number in the subscript is the maximum value of the sliding window. Before being stored in a number of the subscript, we must first determine whether the queue is digitally less than the number to be deposited. If you already have numbers less than the number to be stored, then these numbers have been impossible is the maximum value of the sliding window, so they will be successively removed from the tail of the queue. Meanwhile, the head of the queue if the number has been slipping out of the window, then slide out the numbers also need to be removed from the head of the queue. Thus, each digital head of the queue is the maximum value of the sliding window.

Title two, maximum queue

Description Title: define and implement a queue to obtain the maximum value of the function max queue, requiring the function max, time complexity push_back, pop_front are O (1).

Thinking :( both ends open queue) we use queue (subsidiary queue) a open at both ends, it is possible to save in the queue is the maximum number and subscripts; and subscripts a variable index.

push function: before being stored in a queue and the auxiliary digital subscript, we must first determine whether the auxiliary queue numbers less than the number has to be deposited. If you already have numbers less than the number to be stored, then these numbers have been impossible is the maximum value of the queue, they will be successively removed from the tail of the auxiliary queue.

Thereafter, the index is stored for the digital and tail of the queue and the tail of the subsidiary queue, and the subscript index by one.

pop functions: If the index index and the auxiliary head of the queue head of the queue is equal, then the head will be simultaneously deleted. Otherwise, just delete the head of the queue.

max function: If the secondary queue is not empty, the head of the queue returns digital assistants.

END

Guess you like

Origin blog.csdn.net/Dora_5537/article/details/90772132