Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
super-egg-drop.cpp 891 Bytes
Copy Edit Raw Blame History
kamyu authored 2018-08-12 12:58 +08:00 . Create super-egg-drop.cpp
// Time: O(klogn)
// Space: O(1)
class Solution {
public:
int superEggDrop(int K, int N) {
int left = 1, right = N;
while (left <= right) {
const auto mid = left + (right - left) / 2;
if (check(mid, K, N)) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return left;
}
private:
bool check(int n, int K, int N) {
// Each combinatin of n moves with k broken eggs could represent a unique F.
// Thus, the range size of F that all cominations can cover
// is the sum of C(n, k), k = 1..K
int total = 0, c = 1;
for (int k = 1; k <= K; ++k) {
c *= n - k + 1;
c /= k;
total += c;
if (total >= N) {
return true;
}
}
return false;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

Search