2 mobile phone questions and promotion on floor 5, 100 (Baidu interview questions)

Source: Internet
Author: User

One floor has 100 floors. It is known that a cell phone will break down one of them. Now there are two identical mobile phones. Excuse me, it can be flushed at least a few times to ensure that the limit floor value is found. (it must be broken when a layer is dropped from Layer 3 ).

We use Q (n) to list the problems with N mobile phones. We will consider them separately.


What does the so-called minimum number of times mean?


The so-called minimum number of times refers to an algorithm based on which, in any case, can be found within a given number of steps (no matter how many floors the mobile phone's limit floor is ). some people may say that it is at least once, because for example, if I dropped my cell phone down from the first floor and the cell phone is broken for the first time, it means it only takes at least once. in fact, this is not the minimum meaning of the question. what if I leave my cell phone on the first floor? Is this the same time? Therefore, at least once is not true. understanding the minimum number of times does not mean the minimum number of times (it must be 1) in a special case. Instead, make sure that the floor value of the limit (regardless of the number of floors) is located within the minimum number of times obtained ).

Information implied by the question:

In fact, the question implies a major piece of information, that is, if the mobile phone is not broken, it can be used for the second time. If it is broken, it cannot be used. the reason is that you cannot drop a bad cell phone to determine whether the current floor has exceeded the maximum limit.

Q (1) Analysis:

When there is only one mobile phone, we obviously can only try it from the first floor to the first floor. In the worst case, it will always go to the second floor. That is to say, our Q (1) = 100. why? Apparently, if we
If the cell phone is not broken in the building X, the floor we lost in the next time must be larger than that in the building X (otherwise it makes no sense because the building X is not broken, it will not be bad if it is smaller than the X building ). in addition, it will certainly not be greater than x + 1. Otherwise, set the next floor to Y (Y> x + 1). If the phone breaks down the next time you leave the floor on layer y, in this case, we cannot determine x + 1, x + 2 ...... y-1 which floor of this y-x-1 (greater than 1) is the limit value of the phone, because we have no phone.

Therefore, if there is only one mobile phone, we can only drop it once from 1 to 100. in other words, if we know that a cell phone will break down at a layer of X and Y, we only need to try from X to Y one by one. That is to say, we need to test y-x + once. if we have k mobile phones and the maximum floor range of a mobile phone that can be determined t times is F (K, T), then f (1, t) = T. (from the discussion above ).

Q (2) Analysis:

We might as well set up to lose n times. and the sequence of the first mobile phone is (assume that if the first mobile phone is not broken, if it is broken in XK, it will not be used again, now we know that the phone is broken between the X (k-1) and X (k) layers.

Obviously, it must be X (k)-X (k-1) + k <= N, because if the first phone is lost on the X (k) layer, then you know that the phone is in [x (k-1) + 1,
A certain floor between X (k)] is broken or not, that is, we can traverse it. At this time, because there is one cell phone left, we must lose X (k) -X (k-1) times, and since X 1 has been lost to X (k), the first phone has been lost K times, so a total of X (k)-X (k-1) + K times, because it has been assumed that at least N times can be done, so there are X (k)-X (k-1) + k <= n. so we know that X (k) <= N + (n-1) + (n-2) +... + (n-k + 1) obviously x sequence must not be less than 100, that is, N + (n-1) + (n-2)
+... + 1> = 100, that is, the smallest n must satisfy n> = 14. The first lost mobile phone sequence is 14, 27, 39, 50, 60, 69, 77, 84, 90, 95, 99,100, so you can know that if the phone breaks down for the first time, you will lose 13 times between [1, 13] with the second phone and add the first phone once, 14 times in total. at 27 o'clock, there were 12 + 2 .....

Based on the above analysis, F (2, t) = T * (t + 1)/2; that is, if two mobile phones are lost t times, the possible range is T * (t + 1)/2. Therefore, the minimum number of times must meet T * (t + 1) /2> = 100.

Q (3) Analysis

In the same way, the sequence of the first mobile phone is the same as that of n times, because F (2, t) = T * (t + 1)/2, then there is X (k)-X (k-1) <= f (2,
N-k + 1), knows F (3, t) = F (2, t) + F (2, t-1) + F (2, T-2) + ..... F (2, 1), and finally get the least satisfaction

F (3, T)> = 100 t is what you want.


Q (m) Analysis

In the same way, F (M, t) = f (M, t) + f (M, t-1) + f (M, T-2) +... + f (S-1, 1), and the minimum number of times is equal to or equal to F (M, T)> = 100 t. (This is actually a recursive process)


Reprinted please indicate the source. This is the result of the author's own research, not from other places.

If you have any questions, you can ask.

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.