How hard is it to find the biggest diamond?

Source: Internet
Author: User

How hard is it to find the biggest diamond?

The HUST Microsoft technology club is about to recruit new students. When preparing to recruit new jobs, I took a look at last year's pen questions. The first question of capability testing is as follows:

Elevator problems-a diamond is placed at the door of each floor from the first floor to the tenth floor, with different diamond sizes. When you take the elevator from the first floor to the tenth floor, the elevator door on each floor will open once. You can only get a diamond once. Q: How can you get the largest one? (Tip: there is no perfect strategy, reasonable Choice is a necessary means to solve the problem) This is a classic interview question. As the question says, there is no perfect strategy to make sure you get the biggest diamond. There are many answers that we agree on. For example, select the large, medium, and small standards at Layer 1-3, verify the previous results at Layer 4-6, and make a selection at Layer 7-9, with a minimum protection of Layer 10. Or 1-5 layers to determine the large, medium, and small standards, 6-10 layers can be selected. First, you must correctly understand the meaning of the question. Some overly divergent answers (such as taking the stairs and taking the elevator back and forth) should not be accepted, because these answers already violate the meaning of the question, followed by a detailed question, you need to select at least one gemstone, even if the first-9 layers have missed the second layer and then small, you have to choose not to empty hands. The most important thing is to reflect your strategy and have a solution to the problem, although it may not be correct. Back to the question itself, is there a way for you to get the biggest diamond with a higher probability? Let's take a look. Suppose we choose a floor to get the diamond (this is also a solution, at least it makes us not hesitate, ). Obviously, we have a 10% probability of getting the maximum diamond in this way, which is indeed relatively low. Another method that seems complicated is: we divide the ten floors into two parts: 5 + 5, and the first five layers do not take diamonds. In other words, the first five layers are, you just want to take a look. Then, in the next five layers, you will take the first diamond that is bigger than the first five layers. In this case The biggest one of all the diamonds before the appearance of the largest diamond As B2, As long as B2 is in the first five layers, the biggest diamond will be obtained. In this case, the probability of the greatest diamond is calculated as follows: P = (1/10) * 1 + (1/10) * (5/6) + (1/10) * (5/7) + (1/10) * (5/8) + (1/10) * (5/9) Indicates the probability that B2 appears in the first five layers when the maximum diamond appears in different locations (layer 6-10. The calculated probability is 37.28%! This is a small probability! But is there a more effective way? Careful people will find out, why should we divide the ten floors into five + five parts? Is there a better solution for dividing it into N + (10-n) layers? Based on the previous model, we can easily conclude the following fact: As long as B2 appears on the first N layers, we will surely get the largest diamond. The probability is: P = (1/10) [(1 + N/(n + 1) + N/(n + 2) +... + N/9)] --- (1) it indicates the probability that B2 appears in the first N layers when the largest diamond appears in n + 1 to 10th layers. The formula (1) corresponds to function f (N). MATLAB can be used to draw the approximate curve of this function (you can also use Excel or other mathematical tools ): when N is 3 (n can only be an integer), the maximum probability is 39.87%! This value may be much larger than you think. That is to say, when you are watching the diamond in the first three floors, you can immediately retrieve the diamond that is bigger than the first three. In this way, you get the highest probability of diamond is 39.87%. We can also take a look at the more general situation. Assume that this building has more than 9 floors, and it has an X-layer height, we hope to obtain the relationship between the maximum probability and X. In the same way as Formula 1, the probability of getting the maximum diamond is determined by the formula below: P = 1/X [n/n + N/(n + 1) +... + N/X] -- (2) We can use MATLAB to roughly plot the p-x curve: we can see that when x approaches positive infinity, this probability is close to a value between 0.35 and 0.4 (which we will prove later). We can find this value by finding the extreme value of (2. To facilitate the solution, we assume that G (n) is a continuous function. G (n) = N/X [1/n + 1/(n + 1) +... + 1/(X-1)] The brackets are part of a harmonic series and cannot be directly solved using formulas. However, when x approaches infinity, g (n) can be written in the Form G (n) = N/X [Ln (X-1)-Ln (n)] approximately equal N/X [Ln (x)-Ln (n)] =-(N/x) ln (N/X) --- (3) the right side of the order (3) formula is H (n), and The yi zhi H (n) is a continuous concave function. The first derivative is H '(n) =-(1 + Ln (N/X), so that H '(n) = 0, get n = x/e, bring H (N) h (x/E) = 1/E is about 0.3679. In other words, Even if you make the right decision on a tall building, you may have at least 36.79% chance of getting the maximum diamond. Do you have a better strategy? Welcome to contact me in the message ;-)

Freesc
September 14

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.