LeetCode 题解工作台

鸡蛋掉落

给你 k 枚相同的鸡蛋,并可以使用一栋从第 1 层到第 n 层共有 n 层楼的建筑。 已知存在楼层 f ,满足 0 ,任何从 高于 f 的楼层落下的鸡蛋都会碎,从 f 楼层或比它低的楼层落下的鸡蛋都不会破。 每次操作,你可以取一枚没有碎的鸡蛋并把它从任一楼层 x 扔下(满足 1 )。如果鸡蛋碎了,你…

category

3

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

困难 · 状态·转移·动态规划

bolt

答案摘要

我们设计一个函数 $dfs(i, j)$,表示有 层楼以及 个鸡蛋时,确定 值的最小操作次数,那么答案就是 $dfs(n, k)$。 函数 $dfs(i, j)$ 的执行逻辑如下:

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 状态·转移·动态规划 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你 k 枚相同的鸡蛋,并可以使用一栋从第 1 层到第 n 层共有 n 层楼的建筑。

已知存在楼层 f ,满足 0 <= f <= n ,任何从 高于 f 的楼层落下的鸡蛋都会碎,从 f 楼层或比它低的楼层落下的鸡蛋都不会破。

每次操作,你可以取一枚没有碎的鸡蛋并把它从任一楼层 x 扔下(满足 1 <= x <= n)。如果鸡蛋碎了,你就不能再次使用它。如果某枚鸡蛋扔下后没有摔碎,则可以在之后的操作中 重复使用 这枚鸡蛋。

请你计算并返回要确定 f 确切的值最小操作次数 是多少?

 

示例 1:

输入:k = 1, n = 2
输出:2
解释:
鸡蛋从 1 楼掉落。如果它碎了,肯定能得出 f = 0 。 
否则,鸡蛋从 2 楼掉落。如果它碎了,肯定能得出 f = 1 。 
如果它没碎,那么肯定能得出 f = 2 。 
因此,在最坏的情况下我们需要移动 2 次以确定 f 是多少。 

示例 2:

输入:k = 2, n = 6
输出:3

示例 3:

输入:k = 3, n = 14
输出:4

 

提示:

  • 1 <= k <= 100
  • 1 <= n <= 104
lightbulb

解题思路

方法一:记忆化搜索 + 二分查找

我们设计一个函数 dfs(i,j)dfs(i, j),表示有 ii 层楼以及 jj 个鸡蛋时,确定 ff 值的最小操作次数,那么答案就是 dfs(n,k)dfs(n, k)

函数 dfs(i,j)dfs(i, j) 的执行逻辑如下:

如果 i<1i \lt 1,说明楼层已经小于等于 00,此时返回 00

如果 j=1j = 1,说明只有一个鸡蛋,那么只能从第一层开始一层一层试,最坏情况下需要试 ii 次,此时返回 ii

否则,我们考虑枚举第一个鸡蛋从第 xx 层扔下的情况,其中 1xi1 \le x \le i。如果鸡蛋在第 xx 层扔下时碎了,说明 f<xf \lt x,此时我们接下来需要在 x1x - 1 层下方以及剩下的 j1j - 1 个鸡蛋确定 ff 值,总共需要的最小操作次数为 dfs(x1,j1)+1dfs(x - 1, j - 1) + 1 次;如果鸡蛋在第 xx 层扔下时没碎,说明 f>xf \gt x,此时我们需要在第 x+1x + 1 层及以上以及剩下的 jj 个鸡蛋确定 ff 值,总共需要的最小操作次数为 dfs(ix,j)+1dfs(i - x, j) + 1 次。由于我们要保证最坏情况下操作次数最少,因此 dfs(i,j)=min1ximax(dfs(x1,j1),dfs(ix,j))+1dfs(i, j) = \min_{1 \le x \le i} \max(dfs(x - 1, j - 1), dfs(i - x, j)) + 1

如果按照这样的方式枚举,由于状态数有 n×kn \times k,每个状态需要枚举 nn 次,那么总时间复杂度达到 O(n2×k)O(n^2 \times k),这会超出时间限制,我们考虑如何进行优化。

我们注意到函数 dfs(x1,j1)dfs(x - 1, j - 1) 随着 xx 的增大而单调递增,而函数 dfs(ix,j)dfs(i - x, j) 随着 xx 的增大而单调递减,因此存在一个最优的 xx 值使得 max(dfs(x1,j1),dfs(ix,j))\max(dfs(x - 1, j - 1), dfs(i - x, j)) 达到最小值。我们可以对 xx 进行二分查找,找出这个最优的 xx 值。其中 xx 是满足 dfs(x1,j1)dfs(ix,j)dfs(x - 1, j - 1) \le dfs(i - x, j) 的最大整数。这样我们可以将时间复杂度降低到 O(n×klogn)O(n \times k \log n)

时间复杂度 O(n×klogn)O(n \times k \log n),空间复杂度 O(n×k)O(n \times k)。其中 nnkk 分别是楼层数和鸡蛋数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution:
    def superEggDrop(self, k: int, n: int) -> int:
        @cache
        def dfs(i: int, j: int) -> int:
            if i < 1:
                return 0
            if j == 1:
                return i
            l, r = 1, i
            while l < r:
                mid = (l + r + 1) >> 1
                a = dfs(mid - 1, j - 1)
                b = dfs(i - mid, j)
                if a <= b:
                    l = mid
                else:
                    r = mid - 1
            return max(dfs(l - 1, j - 1), dfs(i - l, j)) + 1

        return dfs(n, k)
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    The candidate demonstrates understanding of dynamic programming and how state transitions work for this problem.

  • question_mark

    The candidate suggests using binary search to optimize the dynamic programming solution, balancing egg usage and floor testing.

  • question_mark

    The candidate is able to explain how the solution scales with increasing egg count and floors, focusing on both time and space efficiency.

warning

常见陷阱

外企场景
  • error

    Mistaking the problem for a linear search, leading to inefficient solutions that do not scale well.

  • error

    Not recognizing that a drop could result in both an egg breaking or surviving, which requires careful state management in the dynamic programming approach.

  • error

    Overcomplicating the problem by trying to test too many floors without leveraging binary search for optimal moves.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Increasing the number of eggs while keeping the number of floors fixed, or vice versa, to test scalability.

  • arrow_right_alt

    Modifying the problem to account for additional constraints, such as a limit on the total number of drops allowed.

  • arrow_right_alt

    Adjusting the solution to handle variations in how eggs are reused or the consequences of breaking them.

help

常见问题

外企场景

鸡蛋掉落题解:状态·转移·动态规划 | LeetCode #887 困难