diff --git a/Algorithms/0887. Super Egg Drop/887. Super Egg Drop.go b/Algorithms/0887. Super Egg Drop/887. Super Egg Drop.go new file mode 100644 index 00000000..7e9758c8 --- /dev/null +++ b/Algorithms/0887. Super Egg Drop/887. Super Egg Drop.go @@ -0,0 +1,37 @@ +package leetcode + +// 解法一 二分搜索 +func superEggDrop(K int, N int) int { + low, high := 1, N + for low < high { + mid := low + (high-low)>>1 + if counterF(K, N, mid) >= N { + high = mid + } else { + low = mid + 1 + } + } + return low +} + +// 计算二项式和,特殊的第一项 C(t,0) = 1 +func counterF(k, n, mid int) int { + res, sum := 1, 0 + for i := 1; i <= k && sum < n; i++ { + res *= mid - i + 1 + res /= i + sum += res + } + return sum +} + +// 解法二 动态规划 DP +func superEggDrop1(K int, N int) int { + dp, step := make([]int, K+1), 0 + for ; dp[K] < N; step++ { + for i := K; i > 0; i-- { + dp[i] = (1 + dp[i] + dp[i-1]) + } + } + return step +} diff --git a/Algorithms/0887. Super Egg Drop/887. Super Egg Drop_test.go b/Algorithms/0887. Super Egg Drop/887. Super Egg Drop_test.go new file mode 100644 index 00000000..d59ebad0 --- /dev/null +++ b/Algorithms/0887. Super Egg Drop/887. Super Egg Drop_test.go @@ -0,0 +1,53 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question887 struct { + para887 + ans887 +} + +// para 是参数 +// one 代表第一个参数 +type para887 struct { + k int + n int +} + +// ans 是答案 +// one 代表第一个答案 +type ans887 struct { + one int +} + +func Test_Problem887(t *testing.T) { + + qs := []question887{ + + question887{ + para887{1, 2}, + ans887{2}, + }, + + question887{ + para887{2, 6}, + ans887{3}, + }, + + question887{ + para887{3, 14}, + ans887{4}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 887------------------------\n") + + for _, q := range qs { + _, p := q.ans887, q.para887 + fmt.Printf("【input】:%v 【output】:%v\n", p, superEggDrop(p.k, p.n)) + } + fmt.Printf("\n\n\n") +} diff --git a/Algorithms/0887. Super Egg Drop/README.md b/Algorithms/0887. Super Egg Drop/README.md new file mode 100755 index 00000000..7ea30b87 --- /dev/null +++ b/Algorithms/0887. Super Egg Drop/README.md @@ -0,0 +1,100 @@ +# [887. Super Egg Drop](https://leetcode.com/problems/super-egg-drop/) + + +## 题目: + +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` + + +## 题目大意 + +你将获得 K 个鸡蛋,并可以使用一栋从 1 到 N 共有 N 层楼的建筑。每个蛋的功能都是一样的,如果一个蛋碎了,你就不能再把它掉下去。你知道存在楼层 F ,满足 0 <= F <= N 任何从高于 F 的楼层落下的鸡蛋都会碎,从 F 楼层或比它低的楼层落下的鸡蛋都不会破。每次移动,你可以取一个鸡蛋(如果你有完整的鸡蛋)并把它从任一楼层 X 扔下(满足 1 <= X <= N)。你的目标是确切地知道 F 的值是多少。无论 F 的初始值如何,你确定 F 的值的最小移动次数是多少? + + +提示: + +1. 1 <= K <= 100 +2. 1 <= N <= 10000 + + +## 解题思路 + +- 给出 `K` 个鸡蛋,`N` 层楼,要求确定安全楼层 `F` 需要最小步数 `t`。 +- 这一题是微软的经典面试题。拿到题最容易想到的是二分搜索。但是仔细分析以后会发现单纯的二分是不对的。不断的二分确实能找到最终安全的楼层,但是这里没有考虑到 `K` 个鸡蛋。鸡蛋数的限制会导致二分搜索无法找到最终楼层。题目要求要在保证能找到最终安全楼层的情况下,找到最小步数。所以单纯的二分搜索并不能解答这道题。 +- 这一题如果按照题意正向考虑,动态规划的状态转移方程是 `searchTime(K, N) = max( searchTime(K-1, X-1), searchTime(K, N-X) )`。其中 `X` 是丢鸡蛋的楼层。随着 `X` 从 `[1,N]`,都能计算出一个 `searchTime` 的值,在所有这 `N` 个值之中,取最小值就是本题的答案了。这个解法可以 AC 这道题。不过这个解法不细展开了。时间复杂度 `O(k*N^2)`。 +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+