LeetCode 题解工作台
鸡蛋掉落
给你 k 枚相同的鸡蛋,并可以使用一栋从第 1 层到第 n 层共有 n 层楼的建筑。 已知存在楼层 f ,满足 0 ,任何从 高于 f 的楼层落下的鸡蛋都会碎,从 f 楼层或比它低的楼层落下的鸡蛋都不会破。 每次操作,你可以取一枚没有碎的鸡蛋并把它从任一楼层 x 扔下(满足 1 )。如果鸡蛋碎了,你…
3
题型
5
代码语言
3
相关题
当前训练重点
困难 · 状态·转移·动态规划
答案摘要
我们设计一个函数 $dfs(i, j)$,表示有 层楼以及 个鸡蛋时,确定 值的最小操作次数,那么答案就是 $dfs(n, k)$。 函数 $dfs(i, j)$ 的执行逻辑如下:
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 状态·转移·动态规划 题型思路
题目描述
给你 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 <= 1001 <= n <= 104
解题思路
方法一:记忆化搜索 + 二分查找
我们设计一个函数 ,表示有 层楼以及 个鸡蛋时,确定 值的最小操作次数,那么答案就是 。
函数 的执行逻辑如下:
如果 ,说明楼层已经小于等于 ,此时返回 ;
如果 ,说明只有一个鸡蛋,那么只能从第一层开始一层一层试,最坏情况下需要试 次,此时返回 ;
否则,我们考虑枚举第一个鸡蛋从第 层扔下的情况,其中 。如果鸡蛋在第 层扔下时碎了,说明 ,此时我们接下来需要在 层下方以及剩下的 个鸡蛋确定 值,总共需要的最小操作次数为 次;如果鸡蛋在第 层扔下时没碎,说明 ,此时我们需要在第 层及以上以及剩下的 个鸡蛋确定 值,总共需要的最小操作次数为 次。由于我们要保证最坏情况下操作次数最少,因此 。
如果按照这样的方式枚举,由于状态数有 ,每个状态需要枚举 次,那么总时间复杂度达到 ,这会超出时间限制,我们考虑如何进行优化。
我们注意到函数 随着 的增大而单调递增,而函数 随着 的增大而单调递减,因此存在一个最优的 值使得 达到最小值。我们可以对 进行二分查找,找出这个最优的 值。其中 是满足 的最大整数。这样我们可以将时间复杂度降低到 。
时间复杂度 ,空间复杂度 。其中 和 分别是楼层数和鸡蛋数。
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)
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | Depends on the final approach |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- 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.
常见陷阱
外企场景- 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.
进阶变体
外企场景- 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.