Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Description:(PYTHON) Before the launch of the iPhone X, Peiyuan received a task to test the drop resistance of the phone, which means that it can
Description:(PYTHON) Before the launch of the iPhone X, Peiyuan received a task to test the drop resistance of the phone, which means that it can be dropped from a building with N floors without breaking. Since the iPhone is so expensive, the time is limited: Please write a function: def solution(K, N) Under the given number of mobile phones (K) and building height (N), return X that meets the following conditions: No matter which floor the mobile phone will be broken, the answer can be found within X times Among all strategies, the X value is the smallest Assumptions: (1) All phones are of the same quality. (2) If the mobile phone is not damaged after being dropped, it can be reused, but it must be counted as lost once. (3) If the mobile phone is broken on the nth floor, all the above n+1 floors will be broken. (4) The mobile phone may not be broken on the highest floor, or it may be broken on the first floor. Because there is only one mobile phone, it will be gone if it breaks, so the first test must be dropped from the first floor. (Otherwise, if it is dropped from the second floor or higher and it breaks, it is not known whether it will break if dropped from the first floor). In the same way, the second test must be dropped from the second floor, and the third test must be dropped from the third floor... . Because there are 14 floors in total, the worst case must be tested 14 times, and X=14 needs to be returned. E2:K=2,N=14 Because there are two mobile phones, if the first test is dropped from the kth floor, there are two situations: 1. Broken: You must use the remaining mobile phone to test floors 1 to k1 to know which floor was broken. 2. Not broken: It is known that the 1 st to k floors will not be broken. So now it is only necessary to confirm which floor of the k+114 th floor will be broken. (Note: Both mobile phones can still be tested at this time). Whether the algorithm is designed well depends on how you choose k. The best solution for this case is X=5, that is to say, no matter which floor of the 14 th floor will be broken, the answer can be found within 5 times. Please deduce the details by yourself to find out the rules. Input: Given the number of mobile phones (K) and the height of the building (N) are both positive integers Output: Returns X that satisfies the following conditions: 1. No matter which floor the mobile phone will be broken, you can find the answer within X times 2. Among all strategies, the X value is the smallest Sample Input: 3 1000000 Sample Output: 182
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started