Over the past few days, a Google interview question has been very popular on the Forum. The question is as follows: "There is a 100-storey building, and you have two identical glass pawns in your hands. If you drop the pawns from a certain layer of the building, they will be broken. Use the two glass pawns in your hands to find out an optimal strategy to find the critical layer ." My analysis and solutions are provided below.
To get the optimal strategy of two pieces, we first simplify the problem and look at the situation of a piece. If there is only one piece in your hand, you have only one option to know the critical layer: from the second floor, one layer by one until the piece is broken, at this time, the floor on your site is the desired critical level. In the worst case, we need to throw 99-2 + 1 = 98 times. You may wonder why it's not 100-2 + 1 = 99 times, that's because the question has already told us that "dropping the pieces from a certain layer of the building will be broken ", so if layer 99 is not broken, you don't have to go to layer 100-throwing it from there will be broken.
We can see from a Piece's strategy that a piece is enough to answer this question. Now I have another piece. How can I use it? Naturally, we hope to narrow down the scope of this layer of search through this piece. To narrow down the scope, we divide the layers of the entire building into section X, find the critical section in section X, and then find the critical layer Layer layer in the critical section. For example, we can divide the building into four sections, where we threw pawns on layer 25, layer 50, and layer 75 to determine the critical section. If the critical section is on layer 25 to layer 50, we start from layer 26 to find the critical layer.
After analysis, the problem becomes how to determine the number of segments X to minimize the number of pieces to be dropped. In the worst case, to determine the critical segment, we need to throw 100/X-1 times; after determining the critical segment to determine the critical layer, we need to throw the X-1 times. Therefore, the problem becomes the minimum problem of finding the function f (x) = (100/x-1) + (x-1. Evaluate f (x) First, f' (x) = 1-100/X2, and then f' (X) = 0 find the field x = 10 (x =-10 ). Because f (x) has a minimum value and only one resident point, when x = 10, f (x) gets the minimum value, and the minimum value is 18. This answers this question.
In fact, the result of 10 is easy to see directly. When there is only one piece, we divide the entire building into a section with 100 floors. When there are two pawns, we have a lot of division, but no matter how we divide them into K1 segments, if each segment has K2 layers, then there will be k1k2 = 100. In the worst case, we need to throw (k1-1) + (k2-1) times. Therefore, the problem can also be expressed as how to make f (K1, K2) =
K1 + K2 is the minimum. In elementary mathematics, we know that the perimeter of a square is the smallest when the rectangular area is certain. Using this conclusion, we can draw a conclusion that K1 = k2 = 10.
Now the problem has been completely solved, but I still want to extend it into a situation where "m floor n pawns. First, let's look at this problem. Given the M Floor, how many pieces will be "enough". That is to say, no more pieces can speed up the search process. In the method I can think of, the bipartite method should be the best. If we look for it by the bipartite method, we need a ceiling (log2 m) chess piece (ceiling is the entire upward function ), it is useless to exceed this number of pawns.
If n> = ceiling (log2m), use the binary method. Now consider n <
Ceiling (log2m. As we have seen above, when n = 2, the problem can be expressed as F (K1, K2) = under the condition of k1k2 = 100.
The minimum value of K1 + K2. Similarly, in the case of N pawns, the problem can be expressed in k1k2... F (K1, K2 ,..., KN) = k1 + k2 +... + Minimum value of kN. Using the Laplace multiplier method, we can easily find: When k1 = k2 =... = KN = n √ M, this multivariate function gets the most value. N √ m may not be an integer, so this is just a theoretical result.
In another way, the question of N pawns on the m floor is actually how to divide m into N factors and multiply them to minimize the sum of each factor. How to break down M makes policy optimization the key to the problem. The conclusion above shows that we try to make each factor equal or have a small difference, and the result of their addition will be smaller. For example, in the case of three pawns on the fifth floor, 5, 4 should be the best choice.
Taking this into consideration, another problem arises: Is it better to break down M as much as possible? For example, is it better to break 100 into 10, 10, or 10? This is actually a question. If two integers are greater than 1, they are large. Obviously, of course, the accumulation is large, so the more M is decomposed, the better.
Number theory tells us that prime numbers are the basis of integers. All integers can be decomposed into the product of several prime numbers. Therefore, if we use the above method to the extreme, we need to break m into the product of prime numbers. Of course, if there are enough pieces, this is not the optimal method. You can still use the bipartite method for the section of the prime data floor.
Bytes ------------------------------------------------------------------------------------------------------
After the above post, I read some solutions from netizens on csdn and chinaunix forums and found that the above methods are not optimal. It is right to segment the building to narrow down the search scope, and the question is whether the segmentation should be even.
The question requires that the total number of throws should be the least. After segmentation, the total number of throws equals to the number of adjacent segments and the number of critical layers. If we perform even segmentation, we determine that the worst throttling of the critical layer is fixed (9 times). As we determine that the number of throttling of adjacent segments increases, the total number of Throttling is also increasing. In this way, the number of throws varies with the critical segments.
That is why the above method is not optimal: the number of throws is unevenly distributed. It is estimated by the worst case that this method has been done several times. In order to minimize the number of throws in the worst case, we hope that the total number of throws will remain unchanged no matter where the critical segment is located, that is, the number of throws will be evenly distributed.
The next solution is easy to come up with: Since the number of throws in the first step (determining the critical segment) increases, let's take the second step (determining the critical Layer) the number of throws decreases with the increase in the first step. The number of throws in the first step increases at a time, which reduces the number of throws in the second step at a time. Assume that the number of layers to be dropped for the first time is F, which is converted into a mathematical model. F + (F-1) +... + 2 + 1> = 99, that is, F (F + 1)/2> = 99, and the solution result is 14.
It is difficult to extend this method to n (n> 2) pieces. My initial idea is to use a uniform segmentation to find a solution, and then correct the solution to evenly distribute the number of throws. If you are interested in this, consider the specific solution.