The book purchasing algorithm in the beauty of programming. Space complexity O (N), time complexity O (N), challenge

Source: Internet
Author: User

I. Problems

There are a total of five flags in the "Harry Potter" series. Assume that each volume is sold separately for 8 euros. If you purchase two different volumes at a time, you can deduct 5% of the fee, and the three volumes are more. Assume that the discount is as follows:

2 discount 5%

3 discounts: 10%

4 discounts: 20%

5 discounts: 25%

Problem: An algorithm is designed to calculate the lowest price for a batch of books purchased by the reader.

 

Ii. Problem simplification

8 euro is a condition that does not affect the algorithm. Assume that the price of the first book is 1.

Now I am not buying a book in A group (A group refers to a unit that can enjoy the discount, the number of books in the group can be 1, and there is no discount at this time), but a book to buy.

The price for the first purchase is 1.

The price for purchasing the second book is 0.9 (the second book must be different from the first one, and its price is 0.95*2-1 = 0.9)

The price for the Third Purchase is 0.8 (the Third Purchase must be different from the first two, and the price is 0.9*3-0.95*2 = 0.8)

The price for the fourth purchase is 0.5.

The price for the fifth purchase is 0.55.

 

The key to reading a book is that the fifth book is more expensive than the fourth one! As you can imagine, if a store has such a promotion, you will not want to buy the fifth one when you buy four!

So we know that if two people have bought four books and the other has bought three books, now a new guy wants to buy a group with one of them. So this buddy will choose the one who bought 3 instead of 4.

 

Iii. Solution to the Problem

According to the principle of buying a book, we guarantee that "the price of each new book must be the lowest", and finally we can get the lowest total price.

Use the set {N1, N2, N3, N4, N5} to indicate the number of each book, among which N1> = N2> = N3> = N4> = N5

How can we get started? Of course we should start from N1, because there cannot be the same books in the same group, and the number of groups must be equal to N1.

I first prove that the number of groups will not be greater than N1

It takes 3.2 to buy 4 books and 3 to buy 5 books. 75. when I want to buy 20 books, should I buy 5 groups for each group of 4 books or 4 groups for each group of 5 books?

3.2*5> 3.75*5, so in this case, the purchase cost is less in a group of five. Similar proof can be provided in other cases. We can conclude that "do not create a new group as a last resort ".

 

Next we can start to buy books.

I will first buy the first N1 book and open them in a row. When I buy a book, I stack it on the previous book. The stack principle is: 1. The book is not repeated with other books in the stack. 2. The minimum purchase price of the book.

Example {7, 6, 3, 2, 1} given in the book}

I bought the first book first, and the book was placed like this.

N1 N1 N1 N1 N1 N1 N1

Then I started to buy the second book. After I bought it, the pile became like this.

N2 N2 N2 N2 N2 N2

N1 N1 N1 N1 N1 N1 N1

And then buy the third. Here N3 obviously cannot be placed on the last N1, because it costs 0.9, but only 0.8 for N2.

N3 N3

N2 N2 N2 N2 N2 N2

N1 N1 N1 N1 N1 N1 N1

Continue to buy the fourth book, put on N1 requires 0.9, put N2 requires 0.8, put on N3 as long as 0.5, then I decisively put on N3 above

N4 N4

N3 N3

N2 N2 N2 N2 N2 N2

N1 N1 N1 N1 N1 N1 N1

I finally bought the last one! I found that only 0.5 is needed to stack this book on N3, and 0.55 is needed to stack it on N4! Of course, I will not choose the one that costs too much.

N4 N4 N5

N3 N3

N2 N2 N2 N2 N2 N2

N1 N1 N1 N1 N1 N1 N1

 

So I bought the book. I bought three, four, three, two, and one. The purchase method is the same as that in the teaching material.

 

Iv. algorithm time complexity and space complexity

Space complexity: Each book uses one storage space, so the space complexity is O (n)

Time Complexity: Each book is arranged once, so the time complexity is O (n)

 

V. Counterfeit evidence

Because this algorithm is too simple, the author does not believe that Microsoft's people cannot come up with it. It is not applicable in what circumstances!

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.