zoukankan      html  css  js  c++  java
  • 100 floors 2 eggs

    https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Mock%20Interviews/Large%20E-Commerce%20Company/E-Commerce%20Company%20-%20Interview%20Problems%20-%20SOLUTIONS/Phone%20Screen%20-%20SOLUTION.ipynb

    Phone Screen - SOLUTION

    Problem

    A tower has 100 floors. You've been given two eggs. The eggs are strong enough that they can be dropped from a particular floor in the tower without breaking. You've been tasked to find the highest floor an egg can be dropped without breaking, in as few drops as possible. If an egg is dropped from above its target floor it will break. If it is dropped from that floor or below, it will be intact and you can test drop the egg again on another floor.

    Show algorithmically how you would go about doing this in as few drops as possible

    Requirements

    Use paper/pencil or a whiteboard for this problem

     

    Solution

    We've already seen this problem in the Riddles section, here is the answer from that section.(Alternatively just google "2 eggs 100 floors" for a plethora of explanations regarding this same solution

    Start from the 10th floor and go up to floors in multiples of 10.

    If first egg breaks, say at 20th floor then you can check all the floors between 11th and 19th with the second egg to see which floor it will not break.

    In this case, the worst-case number of drops is 19. If the threshold was 99th floor, then you would have to drop the first egg 10 times and the second egg 9 times in linear fashion.

    Best solution: We need to minimize this worst-case number of drops. For that, we need to generalize the problem to have n floors. What would be the step value, for the first egg? Would it still be 10? Suppose we have 200 floors. Would the step value be still 10?

    The point to note here is that we are trying to minimize the worst-case number of drops which happens if the threshold is at the highest floors. So, our steps should be of some value which reduces the number of drops of the first egg.

    Let's assume we take some step value m initially. If every subsequent step is m-1, then,$$m+m−1+m−2+.....+1=n$$

    This is

    $$frac{m∗(m+1)}{2}=n$$

    If n =100, then m would be 13.65 which since we can't drop from a decimal of a floor, we actually use 14.

    So, the worst case scenario is now when the threshold is in the first 14 floors with number of drops being 14.

    Note that this is simply a binary search!

     

    Good Job!

  • 相关阅读:
    json格式转换
    早该知道的7个JavaScript技巧
    SPFA加上SLF时判负环的条件
    HDU 4061 A Card Game
    线性筛法求素数
    STL之deque
    POJ 3219 二项式系数
    HDU 4296 Buildings
    HDU 4292 Food (成都赛区网络赛第五题,拆点网络流)
    拆点网络流(POJ3281)
  • 原文地址:https://www.cnblogs.com/prmlab/p/6960994.html
Copyright © 2011-2022 走看看