"The beauty of Programming" reading notes (iv): The greedy solution to the problem of selling book discounts

Source: Internet
Author: User


"The beauty of Programming" reading notes (iv): The greedy solution to the problem of selling book discounts
Every time I read the questions in the beauty of programming, want to personally calculate or deep thinking, all feel the time passes quickly, every one or two hours, if the code again, the time may take longer, really do not understand the interview through Microsoft's brains of those who are in the end what structure, The preface of the book mentions that they each interview 45 minutes, also must write the procedure?! In my opinion, it might be possible to control the CPU curve or the Chinese chess problem. If it's a book discount, I think it's really not easy, especially if the interviewer gets into the problem of the greedy solution instead of the dynamic programming algorithm, because I wrote this article about 5 hours:-(. But I think as long as the study is not a waste of time, today on the Internet to see Microsoft's campus recruitment site has been updated, and so I read the book, to cast a resume to try:-).
1 Problem description and analysis
The book discount problem description is that a publisher of the "Harry Potter" series has 5 volumes, each is 8 yuan per order, if the reader buy different K (k>=2) volume, you can enjoy different discounts, as follows:


The question is, how do you calculate the maximum number of discounts given an order?
The dynamic programming solution given in the book is no longer discussed here. However, there are two issues that need to be watched separately: (1) If the order is described as (x1,x2,x3,x4,x5), where x1-x5 is the number of the ordered number, which is the number of the volume, namely the first volume X1, the second volume X2, ..., volume 5th X5 ben; If the order is: (X3,X2, X4,X5,X1), the first volume X3, the second volume X2, ..., volume V X1. We can easily see that because each book has the same price, the discount is just about how to choose, not the book. Therefore, the maximum number of discounts on the above two orders is the same, which allows us to use a uniform method (y1,y2,y3,y4,y5), where y1≥y1≥y1≥y1≥y5, to represent an order. The authors set the price of each book to be the same in order to simplify the problem, because if the price of each book is different, the problem becomes more complicated, then we can't just think about selecting a few books, but also consider which ones to choose.

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.