Skip to content

Commit 0dca61f

Browse files
committed
+ problem 887
1 parent daa4f85 commit 0dca61f

File tree

5 files changed

+133
-0
lines changed

5 files changed

+133
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# 887. Super Egg Drop
2+
You are given `k` identical eggs and you have access to a building with `n` floors labeled from `1` to `n`.
3+
4+
You know that there exists a floor `f` where `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**.
5+
6+
Each move, you may take an unbroken egg and drop it from any floor `x` (where `1 <= x <= n`). If the egg breaks, you can no longer use it. However, if the egg does not break, you may **reuse** it in future moves.
7+
8+
Return *the **minimum number of moves** that you need to determine **with certainty** what the value of* `f` is.
9+
10+
#### Example 1:
11+
<pre>
12+
<strong>Input:</strong> k = 1, n = 2
13+
<strong>Output:</strong> 2
14+
<strong>Explanation:</strong>
15+
Drop the egg from floor 1. If it breaks, we know that f = 0.
16+
Otherwise, drop the egg from floor 2. If it breaks, we know that f = 1.
17+
If it does not break, then we know f = 2.
18+
Hence, we need at minimum 2 moves to determine with certainty what the value of f is.
19+
</pre>
20+
21+
#### Example 2:
22+
<pre>
23+
<strong>Input:</strong> k = 2, n = 6
24+
<strong>Output:</strong> 3
25+
</pre>
26+
27+
#### Example 3:
28+
<pre>
29+
<strong>Input:</strong> k = 3, n = 14
30+
<strong>Output:</strong> 4
31+
</pre>
32+
33+
#### Constraints:
34+
* `1 <= k <= 100`
35+
* <code>1 <= n <= 10<sup>4</sup></code>
36+
37+
## Solutions (Python)
38+
39+
### 1. Solution
40+
```Python
41+
class Solution:
42+
@cache
43+
def superEggDrop(self, k: int, n: int) -> int:
44+
if n == 0:
45+
return 0
46+
if k == 1:
47+
return n
48+
49+
x = bisect_right(range(1, n + 1), 0, key=lambda x: self.superEggDrop(k -
50+
1, x - 1) - self.superEggDrop(k, n - x)) + 1
51+
ret = 1 + self.superEggDrop(k - 1, x - 1)
52+
if x > 0:
53+
ret = min(ret, 1 + self.superEggDrop(k, n - x + 1))
54+
55+
return ret
56+
```
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# 887. 鸡蛋掉落
2+
给你 `k` 枚相同的鸡蛋,并可以使用一栋从第 `1` 层到第 `n` 层共有 `n` 层楼的建筑。
3+
4+
已知存在楼层 `f` ,满足 `0 <= f <= n` ,任何从 **高于** `f` 的楼层落下的鸡蛋都会碎,从 `f` 楼层或比它低的楼层落下的鸡蛋都不会破。
5+
6+
每次操作,你可以取一枚没有碎的鸡蛋并把它从任一楼层 `x` 扔下(满足 `1 <= x <= n`)。如果鸡蛋碎了,你就不能再次使用它。如果某枚鸡蛋扔下后没有摔碎,则可以在之后的操作中 **重复使用** 这枚鸡蛋。
7+
8+
请你计算并返回要确定 `f` **确切的值****最小操作次数** 是多少?
9+
10+
#### 示例 1:
11+
<pre>
12+
<strong>输入:</strong> k = 1, n = 2
13+
<strong>输出:</strong> 2
14+
<strong>解释:</strong>
15+
鸡蛋从 1 楼掉落。如果它碎了,肯定能得出 f = 0 。
16+
否则,鸡蛋从 2 楼掉落。如果它碎了,肯定能得出 f = 1 。
17+
如果它没碎,那么肯定能得出 f = 2 。
18+
因此,在最坏的情况下我们需要移动 2 次以确定 f 是多少。
19+
</pre>
20+
21+
#### 示例 2:
22+
<pre>
23+
<strong>输入:</strong> k = 2, n = 6
24+
<strong>输出:</strong> 3
25+
</pre>
26+
27+
#### 示例 3:
28+
<pre>
29+
<strong>输入:</strong> k = 3, n = 14
30+
<strong>输出:</strong> 4
31+
</pre>
32+
33+
#### 提示:
34+
* `1 <= k <= 100`
35+
* <code>1 <= n <= 10<sup>4</sup></code>
36+
37+
## 题解 (Python)
38+
39+
### 1. 题解
40+
```Python
41+
class Solution:
42+
@cache
43+
def superEggDrop(self, k: int, n: int) -> int:
44+
if n == 0:
45+
return 0
46+
if k == 1:
47+
return n
48+
49+
x = bisect_right(range(1, n + 1), 0, key=lambda x: self.superEggDrop(k -
50+
1, x - 1) - self.superEggDrop(k, n - x)) + 1
51+
ret = 1 + self.superEggDrop(k - 1, x - 1)
52+
if x > 0:
53+
ret = min(ret, 1 + self.superEggDrop(k, n - x + 1))
54+
55+
return ret
56+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
@cache
3+
def superEggDrop(self, k: int, n: int) -> int:
4+
if n == 0:
5+
return 0
6+
if k == 1:
7+
return n
8+
9+
x = bisect_right(range(1, n + 1), 0, key=lambda x: self.superEggDrop(k -
10+
1, x - 1) - self.superEggDrop(k, n - x)) + 1
11+
ret = 1 + self.superEggDrop(k - 1, x - 1)
12+
if x > 0:
13+
ret = min(ret, 1 + self.superEggDrop(k, n - x + 1))
14+
15+
return ret

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,7 @@
668668
[884][884l] |[Uncommon Words from Two Sentences][884] |![py]
669669
[885][885l] |[Spiral Matrix III][885] |![rs]
670670
[886][886l] |[Possible Bipartition][886] |![rs]
671+
[887][887l] |[Super Egg Drop][887] |![py]
671672
[888][888l] |[Fair Candy Swap][888] |![rs]
672673
[889][889l] |[Construct Binary Tree from Preorder and Postorder Traversal][889] |![py]
673674
[890][890l] |[Find and Replace Pattern][890] |![py]
@@ -2605,6 +2606,7 @@
26052606
[884]:Problemset/0884-Uncommon%20Words%20from%20Two%20Sentences/README.md#884-uncommon-words-from-two-sentences
26062607
[885]:Problemset/0885-Spiral%20Matrix%20III/README.md#885-spiral-matrix-iii
26072608
[886]:Problemset/0886-Possible%20Bipartition/README.md#886-possible-bipartition
2609+
[887]:Problemset/0887-Super%20Egg%20Drop/README.md#887-super-egg-drop
26082610
[888]:Problemset/0888-Fair%20Candy%20Swap/README.md#888-fair-candy-swap
26092611
[889]:Problemset/0889-Construct%20Binary%20Tree%20from%20Preorder%20and%20Postorder%20Traversal/README.md#889-construct-binary-tree-from-preorder-and-postorder-traversal
26102612
[890]:Problemset/0890-Find%20and%20Replace%20Pattern/README.md#890-find-and-replace-pattern
@@ -4536,6 +4538,7 @@
45364538
[884l]:https://leetcode.com/problems/uncommon-words-from-two-sentences/
45374539
[885l]:https://leetcode.com/problems/spiral-matrix-iii/
45384540
[886l]:https://leetcode.com/problems/possible-bipartition/
4541+
[887l]:https://leetcode.com/problems/super-egg-drop/
45394542
[888l]:https://leetcode.com/problems/fair-candy-swap/
45404543
[889l]:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/
45414544
[890l]:https://leetcode.com/problems/find-and-replace-pattern/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,7 @@
668668
[884][884l] |[两句话中的不常见单词][884] |![py]
669669
[885][885l] |[螺旋矩阵 III][885] |![rs]
670670
[886][886l] |[可能的二分法][886] |![rs]
671+
[887][887l] |[鸡蛋掉落][887] |![py]
671672
[888][888l] |[公平的糖果交换][888] |![rs]
672673
[889][889l] |[根据前序和后序遍历构造二叉树][889] |![py]
673674
[890][890l] |[查找和替换模式][890] |![py]
@@ -2605,6 +2606,7 @@
26052606
[884]:Problemset/0884-Uncommon%20Words%20from%20Two%20Sentences/README_CN.md#884-两句话中的不常见单词
26062607
[885]:Problemset/0885-Spiral%20Matrix%20III/README_CN.md#885-螺旋矩阵-iii
26072608
[886]:Problemset/0886-Possible%20Bipartition/README_CN.md#886-可能的二分法
2609+
[887]:Problemset/0887-Super%20Egg%20Drop/README_CN.md#887-鸡蛋掉落
26082610
[888]:Problemset/0888-Fair%20Candy%20Swap/README_CN.md#888-公平的糖果交换
26092611
[889]:Problemset/0889-Construct%20Binary%20Tree%20from%20Preorder%20and%20Postorder%20Traversal/README_CN.md#889-根据前序和后序遍历构造二叉树
26102612
[890]:Problemset/0890-Find%20and%20Replace%20Pattern/README_CN.md#890-查找和替换模式
@@ -4536,6 +4538,7 @@
45364538
[884l]:https://leetcode.cn/problems/uncommon-words-from-two-sentences/
45374539
[885l]:https://leetcode.cn/problems/spiral-matrix-iii/
45384540
[886l]:https://leetcode.cn/problems/possible-bipartition/
4541+
[887l]:https://leetcode.cn/problems/super-egg-drop/
45394542
[888l]:https://leetcode.cn/problems/fair-candy-swap/
45404543
[889l]:https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-postorder-traversal/
45414544
[890l]:https://leetcode.cn/problems/find-and-replace-pattern/

0 commit comments

Comments
 (0)