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
|
class Solution:
def superEggDrop(self, K: int, N: int) -> int:
cache = {}
def dp(k,n):
if k==1: return n
if n==0: return 0
if (k,n) in cache:
return cache[(k,n)]
# res = float('inf')
# for x in range(1, n+1):
# res = min(max(dp(k-1, x-1),dp(k, n-x))+1,res)
res = float('inf')
lo, hi = 1, n
while lo <= hi:
mid = (lo+hi) // 2
broken = dp(k - 1, mid - 1)
not_broken = dp(k, n - mid)
if broken > not_broken:
hi = mid - 1
res = min(res, broken + 1)
else:
lo = mid + 1
res = min(res, not_broken + 1)
cache[(k,n)] = res
return res
return dp(K, N)
|