forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 15
/
super-egg-drop.cpp
35 lines (33 loc) · 891 Bytes
/
super-egg-drop.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// 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;
}
};