【leetcode每日刷题】887. Super Egg Drop

使用K个鸡蛋来确定F(0<=F<=N),F表示(鸡蛋若从高于F的地方掉下会摔坏,在低于F的地方不会摔坏),问最少需要丢几次鸡蛋可以确定F的值。

You are given K eggs, and you have access to a building with N floors from 1 to N

Each egg is identical in function, and if an egg breaks, you cannot drop it again.

You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break.

Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N). 

Your goal is to know with certainty what the value of F is.

What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?

Example 1:

Input: K = 1, N = 2
Output: 2
Explanation: 
Drop the egg from floor 1.  If it breaks, we know with certainty that F = 0.
Otherwise, drop the egg from floor 2.  If it breaks, we know with certainty that F = 1.
If it didn't break, then we know with certainty F = 2.
Hence, we needed 2 moves in the worst case to know what F is with certainty.

Example 2:

Input: K = 2, N = 6
Output: 3

Example 3:

Input: K = 3, N = 14
Output: 4
Note:
  1. 1 <= K <= 100
  2. 1 <= N <= 10000
class Solution(object):
    def superEggDrop(self, K, N):
        """
        :type K: int
        :type N: int
        :rtype: int
        """
        dp = [[0 for i in range(K+1)] for j in range(N+1)]
        m = 0
        while dp[m][K] < N:
            m += 1
            for k in range(1, K+1):
                dp[m][k] = dp[m-1][k-1] + dp[m-1][k] + 1
        return m


if __name__ == "__main__":
    K = 3
    N = 14
    solution = Solution()
    print(solution.superEggDrop(K, N))

使用dp[m][k]表示k个鸡蛋用来试验m次可以检测的最大高度,1个鸡蛋丢一次可以确定的F最大值为1,使用两个鸡蛋丢一次也是只能最大确定1;使用1个鸡蛋丢两次可以最大确定2(在1层丢下没有坏,在2层丢下坏掉),

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值