forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
super-egg-drop.py
32 lines (29 loc) · 871 Bytes
/
super-egg-drop.py
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
# Time: O(klogn)
# Space: O(1)
class Solution(object):
def superEggDrop(self, K, N):
"""
:type K: int
:type N: int
:rtype: int
"""
def check(n, K, 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
total, c = 0, 1
for k in xrange(1, K+1):
c *= n-k+1
c //= k
total += c
if total >= N:
return True
return False
left, right = 1, N
while left <= right:
mid = left + (right-left)//2
if check(mid, K, N):
right = mid-1
else:
left = mid+1
return left