Hold down the eggs

Source: Internet
Author: User

Source of question: "waiting for words", original @ Chen liren. You are welcome to continue to pay attention to the Public Account "" waiting for words"

Two eggs with the same soft and hard degree but unknown may be broken on the first floor, or they may fall down from the second floor. A 100-storey building requires you to use the two eggs to determine which layer is the highest place where the eggs can safely fall. Two eggs can be broken.

Method Analysis

The safest way to see this question is to test it one by one, but you only need one egg. We now have two eggs, which can be used in a faster way.

Further? The possible test method is binary search. For example, if the first egg is dropped on 50 layers, the second egg is tested layer by layer from 1 to 49. If the egg is not broken, the first egg is dropped on layer 75. If it is broken, the second egg is tested layer by layer from layer 51-74... However, this method is prone to tragedy. For example, when layer 49 is secure, you need to try it 50 times. The effect is worse than only one egg.

The above analysis is from the perspective of eggs. It seems difficult to get the minimum number of attempts. What if we change the angle from the perspective of every floor in height? If a floor can be safely dropped, how many attempts are required? See the following analysis.

In the process of programming and solving the problem, if we encounter the optimal problem, we can try the dynamic planning method first. In the dynamic planning method, we must first find the optimal subproblem that constitutes the optimal problem. Therefore, in the following analysis, we first try the dynamic planning method and how to solve this problem. This is also a typical programmer's idea. Secondly, among many problems, many of them can be directly attributed to mathematical equations. If we can write mathematical equations, the answer will be more concise and wonderful. Therefore, the second method will try to sum up the mathematical equation.

Dynamic Planning-based approach

As mentioned above, to adopt dynamic planning, it is most important to find Sub-problems. In the following analysis, f {n} indicates that the eggs are dropped from the nth floor, and no minimum attempts are made. The first egg may fall at the position (1, N). The first egg is dropped from the layer I. There are two situations:

  1. Broken, second egg, need to test from the first level, there is a I-1 chance

  2. No broken, two eggs, n-I layers. This is a sub-problem f {n-I}. Therefore, when the first egg is dropped from the I position, the number of attempts is 1 + max (I-1, f {n-I}), the minimum number of attempts for each I is the value of f {n. The state transition equation is as follows: f {n} = min (1 + max (I-1, F {n-1}) where: The range of I is (1, N ), f {1} = 1.

The Code is as follows:

Int twoegg (int n) {vector <int> dp (n + 1, 0); int I, j; for (I = 1; I <= N; I ++) // for each layer {DP [I] = I; for (j = 1; j <= I; j ++) {DP [I] = min (max (J-1, DP [I-j]) + 1, DP [I]); // dynamic transfer equation} return DP [N];}

Promotion

The dynamic planning method can be promoted to N floors and M eggs. For example, f {n, m} indicates the minimum number of attempts to find the highest floor when there are n floors and M eggs. When the first egg is dropped from the I-level, if it is broken, there will be 1-1 egg left, to determine the safe floor in the floor below, also need f {M-1} Times, find the sub-problem. If it is not broken down, there will be n-I layer above, and another sub-problem will need to be f [n-I, m. The state transition equation is as follows: f {n, m} = min (1 + max (f {n-1, m-1}, f {n-I, m}) where: I is (1, N), f {I, 1} = 1

The Code is as follows:

Int megg (int n, int m) // n layers, M eggs {int I, J, K, max = numeric_limits <int >:: max (); int ** dp = new int * [n + 1]; for (I = 0; I <= N; I ++) {DP [I] = new int [M + 1];} memset (DP [0], 0, sizeof (INT) * (m + 1 )); for (I = 1; I <= N; I ++) DP [I] [1] = I; // initialize for (I = 1; I <= N; I ++) // each layer {for (j = 2; j <= m; j ++) // For K eggs {DP [I] [J] = I; for (k = 1; k <= I; k ++) // Fall From the K layer {DP [I] [J] = min (max (DP [k-1] [J-1], DP [I-K] [J]) + 1, DP [I] [J]) ;}} int res = DP [N] [m]; for (I = 0; I <= N; I ++) Delete [] DP [I]; Delete [] DP; return res ;}

Mathematical equation-Based Method

If the minimum number of attempts is X, the first egg must be dropped from layer X, because there is still X-1 floor in front of the broken egg. If not broken, there are X-1 next to the opportunity. If it is not broken, the first egg can be tried from the X + (X-1) layer for the second time. Why is X-1 added, the first egg is broken, and the second egg can also be tried from x + 1 to X + (X-1)-1 layers, X-2 times. If it hasn't been broken, the first egg, the third attempt from the X + (X-1) + (X-2) layer. Split or not broken, there are X-3 attempts, and so on. So what is the highest floor for the minimum number of attempts? X + (X-1) + (X-2) +... + 1 = x (x + 1)/2. Then, how many times does it take for the highest floor to be 100 floors? X (x + 1)/2> = 100, get x> = 14, try at least 14 times.



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.