[ACM Training] Algorithm primary data structure stack stack+ queue (base + Advanced +poj 2442+1442)

Source: Internet
Author: User

Again face like stacks and queues such a fairly basic data structure of learning, should be from many aspects, multi-dimensional to learn.

First of all, these two data structures are more commonly used, in the standard library has a corresponding structure can be used directly, so the first stage should be learned directly to use, the next stage to explore the specific implementation, as well as the basic structure of the transformation!

In the C + + standard library

Here's a classic interview topic about stacks and queues:

Title: Implement a stack, with a stack (pop), into the stack (push), take the minimum element (Getmin) three methods. To ensure that the time complexity of these three methods is O (1).

Idea: The focus is the design of the Getmin () function, the general idea is to design an additional shaping variable to hold the minimum value of the index value, each time the stack is said to be the minimum value compared with the value of the stack, if the smaller the update.

Myth: This idea of a very critical thinking is that, do not consider the case of the stack, if it happens to be the minimum value out of the stack, then can not get the minimum value!

Further thinking: it is necessary to save the minimum value of the entire process, set an additional stack to hold the smallest value in order, so that even if the current minimum value out of the stack, then the second small value will become the minimum value, still on the stack top of the auxiliary stack.

misunderstanding: Think about the process of this design, if sequentially into the stack increment of a sequence, such as 3,4,5,6,7, then in the auxiliary stack can only record the next 3 as the minimum value to save, then the direct use of getmin after 3 on the stack, then the auxiliary stack there is no minimum value of the information!

Further: To address this vulnerability, you can use:

1, the secondary stack is not only to save the minimum backup, but to save a copy of the original data in the order of all the worth of copies, only so that the getmin once before the loss of the minimum information.

2, the use of auxiliary stacks, first of all to clear the stack is the first into the structure, for 3,4,5 this stack structure, can not directly out of the stack 3 or 4, must first out of the Stack 5 can!!!

So the auxiliary stack with the data stack at the same time out of the stack or into the stack without a direct ordering to save all the data, you can follow the following ideas: (see from here)

Expand:

Implement a queue, with out of the team (DeQueue), queue (EnQueue), take the minimum element (Getmin) three methods. Make sure that the three methods are as small as possible in their time complexity.

[ACM Training] Algorithm primary data structure stack stack+ queue (base + Advanced +poj 2442+1442)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.