美文网首页
254.Drop Eggs

254.Drop Eggs

作者: 博瑜 | 来源:发表于2017-07-20 21:21 被阅读0次

There is a building of n floors. If an egg drops from the k th floor or above, it will break. If it's dropped from any floor below, it will not break.

You're given two eggs, Find k while minimize the number of drops for the worst case. Return the number of drops in the worst case.

Clarification
For n = 10, a naive way to find k is drop egg from 1st floor, 2nd floor ... kth floor. But in this worst case (k = 10), you have to drop 10 times.

Notice that you have two eggs, so you can drop at 4th, 7th & 9th floor, in the worst case (for example, k = 9) you have to drop 4 times.

Example
Given n = 10, return 4.
Given n = 100, return 14.

public class Solution {
/**
 * @param n an integer
 * @return an integer
 */
public int dropEggs(int n) {
    // Write your code here
    long sum = 0;
    int inc = 1;
    while (sum < n) {
        sum += inc;
        inc++;
    }
    return inc - 1;
}
}
//假设最少drop x次,则必须从x层开始扔,因为当第一个蛋碎后,第二个蛋还需要扔 x - 1 次。蛋不碎,还得扔 x - 1次,所以得从 x + x - 1层开始扔。 x + (x - 1) + (x - 2) ... + 1 >= 100

以下做法超时:

public class Solution {
/**
 * @param n an integer
 * @return an integer
 */
public int dropEggs(int n) {
    // Write your code here
    int[] state = new int[n + 1];
    state[1] = 1;
    for (int i = 2; i < n + 1; i++) {
        int min = Integer.MAX_VALUE;
        for (int j = 1; j < i + 1; j++) {
            min = Math.min(Math.max(j - 1, state[i - j]),min);
        }
        state[i] = min + 1;
    }
    return state[n];
}
}
// state(N) = min{max(t - 1, state(N - t))} + 1 | t = 1 .... N      

相关文章

  • 254.Drop Eggs

    There is a building of n floors. If an egg drops from the...

  • How do I make tomato and egg noo

    First, buy some eggs and brake three eggs into the bowl. ...

  • How to make tomato and egg noodl

    First,Scatter eggs Nest,Stir fry tomatoes and eggs in a p...

  • egg的复数怎么读

    egg的复数形式是eggs。音标:[eɡz]。短语:a dozen eggs,一打鸡蛋。例句:Eggs are g...

  • 2018-02-23

    def spam():"""Prints 'Eggs!'"""print "Eggs!"spam() 注意别漏了这...

  • 2018-02-17

    def spam():eggs = 12return eggs print spam() codecademy 1...

  • Eggs Dropping puzzle(2 eggs, 100

    题目如下: You are given two eggs, and access to a 100-storey ...

  • hen eggs

    SUPPORT: huanglittledragon@gmail.com Protect the egg from...

  • Dragons’ Eggs

    本周继续阅读这一系列中的另外一本英文小说。 这本书的内容似乎就与我们有一些的遥远了。故事发生在非洲,一个普普通通的...

  • Easter eggs

    正文 The case for Easter eggs and other treats 为“复活节彩蛋”和其他意...

网友评论

      本文标题:254.Drop Eggs

      本文链接:https://www.haomeiwen.com/subject/oswjkxtx.html