-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path16.py
More file actions
33 lines (26 loc) · 935 Bytes
/
16.py
File metadata and controls
33 lines (26 loc) · 935 Bytes
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
class Solution:
def superEggDrop(self, k: int, n: int) -> int:
def helper(egg, floor):
if egg == 1:
return floor
if floor == 1 or floor == 0:
return floor
if dp[egg][floor] != -1:
return dp[egg][floor]
low = 1
high = floor
minn = float('inf')
while low <= high:
mid = (high+ low)//2
breaks = helper(egg - 1, mid - 1)
not_break = helper(egg, floor - mid)
worst_case = 1 + max(breaks, not_break)
minn = min(minn, worst_case)
if breaks > not_break:
high = mid - 1
else:
low = mid + 1
dp[egg][floor] = minn
return minn
dp = [[-1]*(n + 1) for _ in range(k+1)]
return helper(k, n)