Datasets:

inputs
stringlengths
50
14k
targets
stringlengths
4
655k
Given 3 positives numbers a, b and c. Return the minimum flips required in some bits of a and b to make ( a OR b == c ). (bitwise OR operation). Flip operation consists of change any single bit 1 to 0 or change the bit 0 to 1 in their binary representation.   Example 1: Input: a = 2, b = 6, c = 5 Output: 3 Explanation: After flips a = 1 , b = 4 , c = 5 such that (a OR b == c) Example 2: Input: a = 4, b = 2, c = 7 Output: 1 Example 3: Input: a = 1, b = 2, c = 3 Output: 0   Constraints: 1 <= a <= 10^9 1 <= b <= 10^9 1 <= c <= 10^9
# a, b, c (i subscript) # c = 0 # -> a + b = 2 -> 2 flips # -> a + b = 1 -> 1 flip # -> a + b = 0 -> 0 flips # c = 1 # -> a + b = 2 -> 0 flips # -> a + b = 1 -> 0 flips # -> a + b = 0 -> 1 flip class Solution: def minFlips(self, a: int, b: int, c: int) -> int: res = 0 for i in range(32): mask = 1 << i ai = a & mask bi = b & mask ci = c & mask if ci == 0: if ai and bi: res += 2 elif ai or bi: res += 1 else: if not ai and not bi: res += 1 return res
Given 3 positives numbers a, b and c. Return the minimum flips required in some bits of a and b to make ( a OR b == c ). (bitwise OR operation). Flip operation consists of change any single bit 1 to 0 or change the bit 0 to 1 in their binary representation.   Example 1: Input: a = 2, b = 6, c = 5 Output: 3 Explanation: After flips a = 1 , b = 4 , c = 5 such that (a OR b == c) Example 2: Input: a = 4, b = 2, c = 7 Output: 1 Example 3: Input: a = 1, b = 2, c = 3 Output: 0   Constraints: 1 <= a <= 10^9 1 <= b <= 10^9 1 <= c <= 10^9
class Solution: def minFlips(self, a: int, b: int, c: int) -> int: stra = '{0:b}'.format(a) strb = '{0:b}'.format(b) strc = '{0:b}'.format(c) n = max(len(stra),len(strb),len(strc)) stra = '0' * (n-len(stra)) + stra strb = '0' * (n-len(strb)) + strb strc = '0' * (n-len(strc)) + strc boolA = [s=='1' for s in stra] boolB = [s=='1' for s in strb] boolC = [s=='1' for s in strc] x = 0 for i in range(n): A, B, C = boolA[i], boolB[i], boolC[i] if not ((A or B) == C): if A and B: x += 2 else: x += 1 return x
Given 3 positives numbers a, b and c. Return the minimum flips required in some bits of a and b to make ( a OR b == c ). (bitwise OR operation). Flip operation consists of change any single bit 1 to 0 or change the bit 0 to 1 in their binary representation.   Example 1: Input: a = 2, b = 6, c = 5 Output: 3 Explanation: After flips a = 1 , b = 4 , c = 5 such that (a OR b == c) Example 2: Input: a = 4, b = 2, c = 7 Output: 1 Example 3: Input: a = 1, b = 2, c = 3 Output: 0   Constraints: 1 <= a <= 10^9 1 <= b <= 10^9 1 <= c <= 10^9
class Solution: def minFlips(self, a: int, b: int, c: int) -> int: flips = 0 while a or b or c: if c & 1 == 0: if a & 1: flips += 1 if b & 1: flips += 1 else: if (a & 1) == 0 and (b & 1) == 0: flips += 1 a >>= 1 b >>= 1 c >>= 1 return flips
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: def f(t): a=0 r=1 for i in range(1, K+1): r *= (t-i+1) r//=i a+=r if a>=N: break return a l, h= 1, N while l<h: m=(l+h)//2 if f(m)<N: l=m+1 else: h=m return l
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: def aux(t, k): ret = 0 r = 1 for i in range(1, K+1): r *= t-i+1 r //= i ret += r if ret >= N: break return ret l, r = 1, N while l < r: m = (l + r) // 2 if aux(m, K) < N: l = m+1 else: r = m return l # memo = {} # def dp(n, k): # if (n, k) not in memo: # if n == 0: # ret = 0 # elif k == 1: # ret = n # else: # lo, hi = 1, n # while lo+1 < hi: # x = (lo + hi) // 2 # t1 = dp(n-x, k) # t2 = dp(x-1, k-1) # if t1 < t2: # hi = x # elif t1 > t2: # lo = x # else: # lo = hi = x # ret = 1 + min(max(dp(n-x, k), dp(x-1, k-1)) for x in (lo, hi)) # memo[n, k] = ret # return memo[n, k] # return dp(N, K)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: # M x K --> Given M moves and K eggs, what is the maximum floor we can check ? M = 300 # big enough number dp = [[0 for j in range(K+1)] for i in range(M+1)] # Initialization 1 --> no move no floor --> dp[0][*] = 0 # Initialization 2 --> no egg no floor --> dp[*][0] = 0 # General case --> we want to find dp[m][k] --> we pick one egg and drop (1 move) # --> now we have k or k-1 eggs, depending on whether the previous egg is broken # --> so in either case, we can at least sum up 1 (first move) + dp[m-1][k] + dp[m-1][k-1] for i in range(1, M+1): for j in range(1, K+1): dp[i][j] = 1 + dp[i-1][j] + dp[i-1][j-1] if dp[i][j] >= N: return i
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: dp = [[0] * (N+1) for _ in range(K+1)] # dp[k][n]: the max floor number we can cover with k eggs and n drops # if k = 1, dp[1][n] = n # if k >= 2, after one drop, n-1 drops left, two cases: # if the egg breaks, k-1 egges, thus dp[k-1][n-1] # if it doesn't break, k egges, thus dp[k][n-1] # just leave the 0 col and 0 row blank # when k = 1 # when k >= 2 for k in range(1, K+1): for n in range(1, N+1): if k == 1: dp[k][n] = n else: dp[k][n] = 1 + dp[k-1][n-1] + dp[k][n-1] # find the smallest n in the last row with dp[K,n] > N top, bott = 0, N while top < bott: mid = (top + bott) // 2 if dp[K][mid] < N: top = mid + 1 else: bott = mid return bott
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
from functools import lru_cache class Solution: def superEggDrop(self, K: int, N: int) -> int: # time complexity: O(K*N*logN) # space complexity: O(K*N) @lru_cache(None) def dp(k, n): # k eggs, n floors if k == 0: return 0 if k == 1: return n if n <= 1: return n if k > math.log(n, 2): return 1 + int(math.log(n,2)) l, r = 1, n + 1 while l < r: m = l + (r - l) // 2 if dp(k-1, m-1) >= dp(k, n-m): r = m else: l = m + 1 return 1 + max(dp(k-1, l-1), dp(k, n-l)) return dp(K, N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
from functools import lru_cache class Solution: def superEggDrop(self, K: int, N: int) -> int: @lru_cache(None) def dp(k, n): if k == 0: return 0 if k == 1: return n if n <= 1: return n if k > log(n, 2): return 1 + int(log(n, 2)) l, r = 1, n + 1 while l < r: m = l + (r - l) // 2 if dp(k-1, m-1) < dp(k, n-m): l = m + 1 else: r = m return max(1 + dp(k-1, l-1), 1 + dp(k, n-l)) return dp(K, N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: # dp = [[0 for _ in range(N+1)] for _ in range(K+1)] dp = list(range(N+1)) for i in range(2, K+1): k = 1 temp =[0] + [N + 1] * (N) for j in range(1, N+1): while k < j+1 and temp[j-k] > dp[k-1]: k += 1 temp[j] = 1 + dp[k-1] # if i > j: # dp[i][j] = dp[i-1][j] # else: # val = N + 1 # for k in range(1, j+1): # val = min(val, 1 + max(dp[i-1][k-1], dp[i][j-k])) # dp[i][j] = val dp = temp[:] # print(dp) return dp[-1]
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: # dp = [[0 for _ in range(N+1)] for _ in range(K+1)] dp = list(range(N+1)) for i in range(2, K+1): k = 1 temp =[0] + [float('inf')] * (N) for j in range(1, N+1): while k < j+1 and temp[j-k] > dp[k-1]: k += 1 temp[j] = 1 + dp[k-1] # if i > j: # dp[i][j] = dp[i-1][j] # else: # val = N + 1 # for k in range(1, j+1): # val = min(val, 1 + max(dp[i-1][k-1], dp[i][j-k])) # dp[i][j] = val dp = temp[:] # print(dp) return dp[-1]
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: dp = range(N+1) for i in range(2, K+1): k = 1 temp =[0] + [N + 1] * (N) for j in range(1, N+1): while k < j+1 and temp[j-k] > dp[k-1]: k += 1 temp[j] = 1 + dp[k-1] dp = temp[:] return dp[-1]
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: self.memo = {} return self.cal_eggs(K, N) def find_that_floor(self,K,N): left, right = 1, N while (left <= right): mid = (left + right) // 2 # if it breaks; r1 = 1 + self.cal_eggs(K - 1, mid - 1) # if it not breaks; r2 = 1 + self.cal_eggs(K, N - mid) if r1 == r2: right = mid; break; elif r1 < r2: left = mid + 1 else: right = mid - 1 return right def cal_eggs(self, K, N): # print(K,N) if N == 0: return 0 if K == 1: return N if (K, N) in self.memo: pass else: r = 2 ** 32 - 1 now_floor = self.find_that_floor(K,N) for floor in range(now_floor, min(now_floor + 2,N+1)): # if it breaks; r1 = 1 + self.cal_eggs(K - 1, floor - 1) # if it not breaks; r2 = 1 + self.cal_eggs(K, N - floor) r = min(r, max(r1, r2)) self.memo[(K, N)] = r return self.memo[(K, N)]
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: max_floors = [[0 for _ in range(N + 1)] for _ in range(K + 1)] for k in range(1, K + 1): for t in range(1, N + 1): max_floors[k][t] = 1 + max_floors[k - 1][t - 1] + max_floors[k][t - 1] for t in range(N): if max_floors[K][t] >= N: return t return N
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def f(self, pivot: int, eggs: int, floors: int) -> int: ans, r = 0, 1 for idx in range(1, eggs+1): r *= (pivot - idx+1) r //= idx ans += r if ans >= floors: break return ans def superEggDrop(self, eggs: int, floors: int) -> int: lo, hi = 1, floors while lo < hi: mi = (lo + hi) // 2 if self.f(mi, eggs, floors) < floors: lo = mi + 1 else: hi = mi return lo
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: from functools import lru_cache @lru_cache(maxsize=None) def dp(i, j): #min number of drops to make with given i eggs and j floors if j==0: return 0 if i==1: return j if j==1: return 1 # print(i, j) #get min value for max(dp(i-1, k-1), dp(i, j-k)) #play with k untill lo, hi = 1, j while lo < hi: k = lo + (hi-lo)//2 if dp(i-1, k-1) < dp(i, j-k): lo = k+1 else: hi = k return 1 + dp(i-1, lo-1) return dp(K, N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
from functools import lru_cache class Solution: def superEggDrop(self, K: int, N: int) -> int: @lru_cache(None) def dp(k: int, n: int) -> int: # k == 0 is invalid but not 'impossible'/infinity. Value 0 ensures formula correctness. # dp[k][n] = min{1 + max(dp[k-1][i], dp[k][n-i]) for i in [1, n]} # dp[0][:] if k <= 0: return 1 << 31 if k == 1: return n if n <= 0: return 0 # Following line is required; o.w. result is not right. # When picking 1st floor to drop egg, if it breaks, no more dropping is needed so dp[:][0] == 0 if n <= 1: return n # Find the first floor where value starts to increase. left, right = 1, n while left <= right: mid = (left + right) // 2 if dp(k - 1, mid - 1) >= dp(k, n - mid): right = mid - 1 else: left = mid + 1 # Compare points. return 1 + dp(k - 1, left - 1) return dp(K, N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
from functools import lru_cache class Solution: def superEggDrop(self, K: int, N: int) -> int: @lru_cache(None) def dp(k: int, n: int) -> int: if k <= 0: return 0 if k == 1: return n # Following line is required; o.w. result is not right. if n <= 1: return n # Find the first floor where dp(k - 1, i - 1) >= dp(k, n - i) # such point must exist because dp(k, n-i) has min as dp(k, 0) == 0 left, right = 1, n while left <= right: mid = (left + right) // 2 if dp(k - 1, mid - 1) >= dp(k, n - mid): right = mid - 1 else: left = mid + 1 return 1 + dp(k - 1, left - 1) return dp(K, N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
from functools import lru_cache class Solution: def superEggDrop(self, K: int, N: int) -> int: # time complexity: O(K*N*logN) # space complexity: O(K*N) @lru_cache(None) def dp(k, n): # k eggs, n floors if k == 0: return 0 if k == 1: return n if n <= 1: return n l, r = 1, n + 1 while l < r: m = l + (r - l) // 2 if dp(k-1, m-1) >= dp(k, n-m): r = m else: l = m + 1 return 1 + max(dp(k-1, l-1), dp(k, n-l)) return dp(K, N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def binary_search(self,A,k,n): lo = 0 hi = n mid = (lo+hi)//2 while lo<hi: if A[k-1][mid-1]==A[k][n-mid]: return mid if A[k-1][mid-1]>A[k][n-mid]: hi = mid else: lo = mid+1 mid = (lo+hi)//2 return lo def superEggDrop(self, K: int, N: int) -> int: if N==1: return 1 # k=0 # n=N # while n>0: # k+=1 # n//=2 # if K>k: # return k A = [[0 for n in range(N+1)] for k in range(K+1)] for n in range(N+1): A[1][n] = n for k in range(K+1): A[k][1] = 1 A[k][2] = 2 for k in range(2,K+1): low=1 for n in range(3,N+1): # linear search # m=1 # while A[k-1][m-1]<A[k][n-m]: # m+=1 #binary search # m = self.binary_search(A,k,n) # A[k][n] = 1+max(A[k-1][m-1],A[k][n-m]) # tip-toe # print('k =',k,' n = ',n,low,' ', A[k-1][low],' ',A[k][n-low-1],' ',A[k][n]) if A[k-1][low]<A[k][n-low-1]: low+=1 A[k][n] = 1+A[k-1][low] # print('k =',k,' n = ',n,low,' ', A[k-1][low],' ',A[k][n-low-1],' ',A[k][n]) # print('') return A[-1][-1]
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: @lru_cache(None) def dp(k, n): if n == 0: ans = 0 elif k == 1: ans = n else: lo, hi = 1, n # keep a gap of 2 X values to manually check later while lo < hi: x = (lo + hi) // 2 t1 = dp(k-1, x-1) t2 = dp(k, n-x) if t1 < t2: lo = x + 1 else: hi = x ans = 1 + max(dp(k-1, lo-1), dp(k, n-lo)) return ans return dp(K,N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def binary_search(self,A,k,n): lo = 0 hi = n mid = (lo+hi)//2 while lo<hi: if A[k-1][mid-1]==A[k][n-mid]: return mid if A[k-1][mid-1]>A[k][n-mid]: hi = mid else: lo = mid+1 mid = (lo+hi)//2 return lo def superEggDrop(self, K: int, N: int) -> int: if N==1: return 1 if K==1: return N A = [n+1 for n in range(N)] At = [1 for n in range(N)] for k in range(1,K): i=0 while At[i]<N: i+=1 At[i] = At[i-1] + A[i-1]+1 if k<K-1: At,A=A,At return i+1 # k=0 # n=N # while n>0: # k+=1 # n//=2 # if K>k: # return k # A = [[0 for n in range(N+1)] for k in range(K+1)] # for n in range(N+1): # A[1][n] = n # for k in range(K+1): # A[k][1] = 1 # A[k][2] = 2 # for k in range(2,K+1): # low=1 # for n in range(3,N+1): # # linear search # # m=1 # # while A[k-1][m-1]<A[k][n-m]: # # m+=1 # #binary search # # m = self.binary_search(A,k,n) # # A[k][n] = 1+max(A[k-1][m-1],A[k][n-m]) # # tip-toe # if A[k-1][low]<A[k][n-low-1]: # low+=1 # A[k][n] = 1+A[k-1][low] return A[-1][-1]
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: memo={} def dp(k,n): if (k,n) not in memo: if n==0: ans=0 elif k==1: ans=n else: lo,hi=1,n while lo+1<hi: x=(lo+hi)//2 t1=dp(k-1,x-1) t2=dp(k,n-x) if t1<t2: lo=x elif t1>t2: hi=x else: lo=hi=x ans=1+min(max(dp(k-1,x-1),dp(k,n-x)) for x in (lo,hi)) memo[k,n]=ans return memo[k,n] return dp(K,N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: dp = [[i for i in range(N+1)]] for i in range(K-1): dp.append( [0] * (N+1)) for i in range(1, K): k = 1 for j in range(1, N+1): while k < j+1 and dp[i][j-k] > dp[i-1][k-1]: k += 1 dp[i][j] = 1 + dp[i-1][k-1] return dp[-1][-1]
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: mem = [[None for _ in range(N)] for _ in range(K - 1)] def dp(k, n, mem): if n <= 1 or k == 1: return n if mem[k-2][n-1] is None: l, r = 1, n while l + 1 < r: i = (l + r) // 2 if dp(k - 1, i - 1, mem) < dp(k, n - i, mem): l = i elif dp(k - 1, i - 1, mem) > dp(k, n - i, mem): r = i else: l = r = i mem[k-2][n-1] = min(max(dp(k - 1, l - 1, mem), dp(k, n - l, mem)), max(dp(k - 1, r - 1, mem), dp(k, n - r, mem))) + 1 return mem[k-2][n-1] return dp(K, N, mem)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: self.memo = {} return self.cal_eggs(K, N) def find_that_floor(self,K,N): left, right = 1, N while (left <= right): mid = (left + right) // 2 # if it breaks; r1 = 1 + self.cal_eggs(K - 1, mid - 1) # if it not breaks; r2 = 1 + self.cal_eggs(K, N - mid) if r1 == r2: return mid elif r1 < r2: left = mid + 1 else: right = mid - 1 return right def cal_eggs(self, K, N): # print(K,N) if N == 0: return 0 if K == 1: return N if (K, N) not in self.memo: r = 2 ** 32 - 1 now_floor = self.find_that_floor(K,N) for floor in range(now_floor, min(now_floor + 2,N+1)): # if it breaks; r1 = 1 + self.cal_eggs(K - 1, floor - 1) # if it not breaks; r2 = 1 + self.cal_eggs(K, N - floor) r = min(r, max(r1, r2)) self.memo[(K, N)] = r return self.memo[(K, N)]
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def binary_search(self,A,k,n): lo = 0 hi = n mid = (lo+hi)//2 while lo<hi: if A[k-1][mid-1]==A[k][n-mid]: return mid if A[k-1][mid-1]>A[k][n-mid]: hi = mid else: lo = mid+1 mid = (lo+hi)//2 return lo def superEggDrop(self, K: int, N: int) -> int: # if N==1: # return 1 if K==1: return N A = [n+1 for n in range(N)] At = [1 for n in range(N)] for k in range(1,K): i=0 while At[i]<N: i+=1 At[i] = At[i-1] + A[i-1]+1 if k<K-1: At,A=A,At return i+1 # k=0 # n=N # while n>0: # k+=1 # n//=2 # if K>k: # return k # A = [[0 for n in range(N+1)] for k in range(K+1)] # for n in range(N+1): # A[1][n] = n # for k in range(K+1): # A[k][1] = 1 # A[k][2] = 2 # for k in range(2,K+1): # low=1 # for n in range(3,N+1): # # linear search # # m=1 # # while A[k-1][m-1]<A[k][n-m]: # # m+=1 # #binary search # # m = self.binary_search(A,k,n) # # A[k][n] = 1+max(A[k-1][m-1],A[k][n-m]) # # tip-toe # if A[k-1][low]<A[k][n-low-1]: # low+=1 # A[k][n] = 1+A[k-1][low] return A[-1][-1]
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution(object): def superEggDrop(self, K, N): dp=[[float('inf')]*(N+1) for _ in range(K+1)] for i in range(1, K+1): dp[i][0] = 0 dp[i][1] = 1 for j in range(1, N+1): dp[1][j] = j for i in range(2, K+1): k = 1 for j in range(2, N+1): while k < j+1 and dp[i][j-k] > dp[i-1][k-1]: k += 1 dp[i][j] = 1 + dp[i-1][k-1] return dp[K][N]
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: dp = [[i for i in range(N+1)]] for _ in range(K-1): dp.append([0, 1] + [0] * (N-1)) for i in range(1, K): k = 1 for j in range(2, N+1): while k < j+1 and dp[i][j-k] > dp[i-1][k-1]: k += 1 dp[i][j] = 1 + dp[i-1][k-1] return dp[-1][-1]
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: dp = [[float('Inf')] * (N + 1) for _ in range(K + 1)] for i in range(1, K + 1): dp[i][0], dp[i][1] = 0, 1 for j in range(1, N + 1): dp[1][j] = j for i in range(2, K + 1): k = 1 for j in range(2, N + 1): while k < j + 1 and dp[i][j-k] > dp[i-1][k-1]: k += 1 dp[i][j] = 1 + dp[i-1][k-1] return dp[K][N]
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: dp=[[float('inf')]*(N+1) for _ in range(K+1)] for i in range(1, K+1): dp[i][0] = 0 dp[i][1] = 1 for j in range(1, N+1): dp[1][j] = j for i in range(2, K+1): k = 1 for j in range(2, N+1): while k < j+1 and dp[i][j-k] > dp[i-1][k-1]: k += 1 dp[i][j] = 1 + dp[i-1][k-1] return dp[K][N]
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
import math from functools import lru_cache class Solution: def superEggDrop(self, K: int, N: int) -> int: @lru_cache(None) def dp(k, n): if k == 0: return 0 if k == 1: return n if n <= 1: return n if k >= math.log(n, 2): return int(math.log(n, 2))+1 ans = sys.maxsize l, r = 1, n + 1 while l < r: m = l + (r - l) //2 if dp(k-1, m-1) >= dp(k, n-m): r = m else: l = m + 1 ans = min(ans, 1 + max(dp(k-1, l-1), dp(k, n-l))) return ans return dp(K, N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: res = [[0]*(N+1) for _ in range(K)] # 初始化第一行 for i in range(N+1): res[0][i] = i; # 从第二行开始 if K == 1: return N for k in range(1,K): j = 1 for i in range(1, N+1): while j < i+1 and res[k-1][j-1] < res[k][i-j]: j += 1 res[k][i] = 1 + res[k-1][j-1] # for j in range(1,i+1): # if res[k-1][j-1] == res[k][i-j]: # res[k][i] = 1 + res[k-1][j-1] # else: # continue # print(res) return int(res[K-1][N])
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: dp = [[float('inf')] * (N+1) for _ in range(K+1)] for k in range(1, K+1): dp[k][1] = 1 dp[k][0] = 0 for n in range(1, N+1): dp[1][n] = n for k in range(2, K+1): x = 1 for n in range(2, N+1): while x < n+1 and dp[k][n-x] > dp[k-1][x-1]: x += 1 dp[k][n] = 1 + dp[k-1][x-1] #print(dp) return dp[-1][-1]
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: @lru_cache(None) def dp(k, n): if n == 0: ans = 0 elif k == 1: ans = n else: lo, hi = 1, n # keep a gap of 2 X values to manually check later while lo < hi: x = (lo + hi) // 2 t1 = dp(k-1, x-1) t2 = dp(k, n-x) if t1 < t2: lo = x + 1 else: hi = x ans = 1 + min(max(dp(k-1, x-1), dp(k, n-x)) for x in (lo, hi)) return ans return dp(K,N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: memo = {} @lru_cache(None) def dp(k, n): if (k, n) not in memo: if n == 0: ans = 0 elif k == 1: ans = n else: lo, hi = 1, n # keep a gap of 2 X values to manually check later while lo < hi: x = (lo + hi) // 2 t1 = dp(k-1, x-1) t2 = dp(k, n-x) if t1 < t2: lo = x + 1 else: hi = x ans = 1 + min(max(dp(k-1, x-1), dp(k, n-x)) for x in (lo, hi)) memo[k, n] = ans return memo[k, n] return dp(K, N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, k: int, n: int) -> int: memo = {} def dp(k, n): if (k, n) not in memo: if n == 0: ans = 0 elif k == 1: ans = n else: l, r = 1, n while l + 1 < r: x = (l + r)//2 t1 = dp(k - 1, x - 1) t2 = dp(k, n - x) if t1 < t2: l = x elif t1 > t2: r = x else: l = r = x ans = 1 + min(max(dp(k-1, x-1), dp(k, n-x)) for x in (l, r)) memo[(k, n)] = ans return memo[(k, n)] return dp(k, n)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
from functools import lru_cache class Solution: def superEggDrop(self, K: int, N: int) -> int: @lru_cache(None) def dp(k, n): if k == 0: return 0 if k == 1: return n if n <= 1: return n ans = sys.maxsize l, r = 1, n + 1 while l < r: m = l + (r - l) //2 if dp(k-1, m-1) >= dp(k, n-m): r = m else: l = m + 1 ans = min(ans, 1 + max(dp(k-1, l-1), dp(k, n-l))) return ans return dp(K, N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: # special case for K = 1 if K == 1: return N # initialize data Rp = [i for i in range(0, N+1)] R = [0] * (N+1) R[1] = 1 k = 2 while k <= K: l1 = 0 for n in range(2, N+1): #if n < 2**k: # R[n] = n.bit_length() if Rp[n-l1-1] >= R[n-1]: R[n] = R[n-1] + 1 else: R[n] = R[n-1] if R[n] > R[n-1]: l1 = n-1 Rp, R = R, Rp k += 1 return Rp[N]
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: dp = [[0 for _ in range(N+1)] for _ in range(K+1)] dp[1] = list(range(N+1)) for i in range(2, K+1): k = 1 for j in range(1, N+1): while k < j+1 and dp[i][j-k] > dp[i-1][k-1]: k += 1 dp[i][j] = 1 + dp[i-1][k-1] # if i > j: # dp[i][j] = dp[i-1][j] # else: # val = N + 1 # for k in range(1, j+1): # val = min(val, 1 + max(dp[i-1][k-1], dp[i][j-k])) # dp[i][j] = val # print(dp) return dp[-1][-1]
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: # we have floors = dp[nummoves][eggs] dp = [[ 0 for egg in range(K+1)] for moves in range(305)] print(N) for move in range(1, 300): for eggs in range(1, K+1): dp[move][eggs] = 1 + dp[move-1][eggs-1] + dp[move-1][eggs] if dp[move][eggs]>=N: return move
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: if N == 1: return 1 eggSolutions = [[None for n in range(N+1)] for k in range(K+1)] for n in range(1, N+1): eggSolutions[1][n] = n for k in range(1, K+1): eggSolutions[k][1] = 1 eggSolutions[k][2] = 2 nextPVals = [i for i in range(N+1)] for k in range(2, K+1): pVals = nextPVals nextPVals = [None, None, 2] pIndex = 2 for n in range(3, N+1): p = pVals[pIndex] minDrops = 1 + max(eggSolutions[k-1][p-1], eggSolutions[k][n-p]) eggSolutions[k][n] = minDrops if eggSolutions[k][n] > eggSolutions[k][n-1]: pIndex += 1 nextPVals.append(n) return eggSolutions[K][N]
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution(object): def superEggDrop(self, K, N): dp = range(N+1) for i in range(2, K+1): k = 1 ndp = [0, 1] + [float('inf')]*(N-1) for j in range(2, N+1): while k < j+1 and ndp[j-k] > dp[k-1]: k += 1 ndp[j] = 1 + dp[k-1] dp = ndp return dp[N]
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: self.memo = {} return self.cal_eggs(K, N) def find_that_floor(self,K,N): left, right = 1, N while (left <= right): mid = (left + right) // 2 # if it breaks; r1 = 1 + self.cal_eggs(K - 1, mid - 1) # if it not breaks; r2 = 1 + self.cal_eggs(K, N - mid) if r1 == r2: return mid elif r1 < r2: left = mid + 1 else: right = mid - 1 return right def cal_eggs(self, K, N): # print(K,N) if N == 0: return 0 if K == 1: return N if (K, N) in self.memo: pass else: r = 2 ** 32 - 1 now_floor = self.find_that_floor(K,N) for floor in range(now_floor, min(now_floor + 2,N+1)): # if it breaks; r1 = 1 + self.cal_eggs(K - 1, floor - 1) # if it not breaks; r2 = 1 + self.cal_eggs(K, N - floor) r = min(r, max(r1, r2)) self.memo[(K, N)] = r return self.memo[(K, N)]
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: @lru_cache(None) def dp(k, n): if n == 0: return 0 if k == 1: return n res = float('inf') lo = 1 hi = n while lo <= hi: mid = (lo + hi) // 2 broken = dp(k - 1, mid - 1) no_broken = dp(k, n - mid) if broken > no_broken: hi = mid - 1 res = min(res, broken + 1) else: lo = mid + 1 res = min(res, no_broken + 1) return res return dp(K, N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: dp = [i for i in range(N+1)] for j in range(1, K): prev = dp.copy() m = 1 for i in range(2, N+1): while m < i and dp[i-m] >= prev[m-1] and dp[i-m-1] >= prev[m]: m += 1 dp[i] = max(dp[i-m], prev[m-1]) + 1 return dp[N]
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: dp = [[0] * (N + 1) for _ in range(K + 1)] for n in range(1, N + 1): dp[1][n] = n for k in range(2, K + 1): dp[k][1] = 1 # dp[k][n] <= n for k in range(2, K + 1): m = 1 for n in range(2, N + 1): while m < n and dp[k - 1][m - 1] < dp[k][n - m]: m += 1 # dp[k][n] = min(max(dp[k - 1][m - 1], dp[k][n - m]) + 1 for m in range(1, n + 1)) # l, r = 1, n # while l < r: # m = (l + r) >> 1 # a, b = dp[k - 1][m - 1], dp[k][n - m] # if a > b: # r = m # elif b > a: # l = m + 1 # else: # break dp[k][n] = max(dp[k - 1][m - 1], dp[k][n - m]) + 1 # for _ in dp: # print(_) return dp[-1][-1]
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
from functools import lru_cache class Solution: def superEggDrop(self, K: int, N: int) -> int: # return self.bottomUp(K, N) return self.findMoves(K, N) @lru_cache(maxsize = None) def findMoves(self, K, N): if K == 1 or N<=1: return N f = N left, right = 1, N while (left < right): mid = (left + right) // 2 broken = self.findMoves(K-1, mid-1) intact = self.findMoves(K, N-mid) if broken > intact: right = mid else: left = mid+1 mid = left -1 return 1 + max(self.findMoves(K-1, mid-1), self.findMoves(K, N-mid)) # intact = # for x in range (1, N+1): # f = min(1 + max(self.findMoves(K-1, x-1), self.findMoves(K, N-x)), f) # return f def bottomUp(self, K, N): dp = [[i for i in range(N+1)] for _ in range(K+1)] for i in range(2, K+1): for j in range(1, N+1): min_drops = N for k in range(1, j+1): min_drops = min(min_drops, 1 + max(dp[i-1][k-1], dp[i][j-k])) dp[i][j] = min_drops return dp[K][N]
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
import numpy as np class Solution: def superEggDrop(self, K: int, N: int) -> int: drops = 0 # the number of eggs dropped floors = [0 for _ in range(K + 1)] # floors[i] is the number of floors that can be checked with i eggs while floors[K] < N: # until we can reach N floors with K eggs for eggs in range(K, 0, -1): floors[eggs] += 1 + floors[eggs - 1] drops += 1 return drops
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop1(self, K: int, N: int) -> int: # 超时!!! memo = [[2**31-1] * (N+1) for _ in range(K+1)] def dp(k, n): if k == 0: return 0 if k == 1: return n if n <= 1: return n if memo[k][n] < 2**31-1: return memo[k][n] ans = memo[k][n] for i in range(1, n+1): ans = min(ans, 1+max(dp(k-1, i-1), dp(k, n-i))) # 极小极大过程!!!alike memo[k][n] = ans return ans return dp(K, N) # D(K-1, i-1), monotonically increasing with i # D(K, N-i) is monotonically decreasing with i! # binary search! # D(K, N) = 1 + min(max(D(K-1, i-1), D(K, N-i))), 1<=i<=N # f(i) = D(K-1, i-1), f(i) is monotonically increasing with i # g(i) = D(K, N-i), g(i) is monotonically decreasing with i # we can use binary search to find i that minimizes max(f(i), g(i)) # # 一个函数单调增;一个函数单调减 # min (max (f, g)) # 就可以使用binary search # f(i) < g(i) 的时候,说明i太小了,需要增大i # f(i) > g(i) 的时候,说明i太大了,需要减少i # 我们需要的是first i, such that f(i) >= g(i) # time complexity = O(KNlogN) smaller than O(K*N*N) # space is same O(KN) def superEggDrop(self, K: int, N: int) -> int: # 超时!!! memo = [[2**31-1] * (N+1) for _ in range(K+1)] def dp(k, n): if k == 0: return 0 if k == 1: return n if n <= 1: return n if memo[k][n] < 2**31-1: return memo[k][n] #ans = memo[k][n] #for i in range(1, n+1): # ans = min(ans, 1+max(dp(k-1, i-1), dp(k, n-i))) # 极小极大过程!!!alike left = 1 right = n + 1 while left < right: mid = left + (right - left) // 2 if dp(k-1, mid-1) >= dp(k, n-mid): right = mid else: left = mid + 1 memo[k][n] = 1 + max(dp(k-1, left-1), dp(k, n-left)) return memo[k][n] return dp(K, N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: dp=[[float('inf')]*(N+1) for _ in range(K+1)] for i in range(1, K+1): dp[i][0] = 0 dp[i][1] = 1 for j in range(1, N+1): dp[1][j] = j for i in range(2, K+1): k = 1 for j in range(2, N+1): while k < j+1 and dp[i][j-k] > dp[i-1][k-1]: k += 1 dp[i][j] = 1 + dp[i-1][k-1] return dp[K][N] # def superEggDrop(self, K: int, N: int) -> int: # dp = [[float('inf') for _ in range(N + 1)] for _ in range(K + 1)] # for i in range(1, K + 1): # dp[i][0] = 0 # dp[i][1] = 1 # for j in range(1, N + 1): # dp[1][j] = j # for i in range(2, K + 1): # for j in range(2, N + 1): # for k in range(1, j + 1): # dp[i][j] = min(dp[i][j], 1 + max(dp[i - 1][k - 1], dp[i][j - k])) # return dp[-1][-1]
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
from math import comb class Solution: def superEggDrop(self, K: int, N: int) -> int: dp={} for i in range(N+1): dp[(i,0)] = 0 for k in range(1,K+1): dp[(1,k)] = 1 for k in range(1,K+1): for n in range(2,N+1): dp[(n,k)] = 1 + dp[(n-1),k] + dp[(n-1),k-1] num = 1 while dp[(num,K)]<N: num+=1 return num
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, eggs: int, floors: int) -> int: dp = [[-1 for _ in range(floors+1)] for _ in range(eggs+1)] # int dp(int k, int n) { # if (k <= 0) return 0; # if (k == 1) return n; # if (n <= 1) return n; # if (m_[k][n] != INT_MIN) return m_[k][n]; # // broken[i] = dp(k - 1, i - 1) is incresing with i. # // unbroken[i] = dp(k, n - i) is decresing with i. # // dp[k][n] = 1 + min(max(broken[i], unbroken[i])), 1 <= i <= n # // find the smallest i such that broken[i] >= unbroken[i], # // which minimizes max(broken[i], unbroken[i]). # int l = 1; # int r = n + 1; # while (l < r) { # int m = l + (r - l) / 2; # int broken = dp(k - 1, m - 1); # int unbroken = dp(k, n - m); # if (broken >= unbroken) # r = m; # else # l = m + 1; # } def search(eggs, floors): if eggs <= 0: return 0 elif eggs == 1: return floors elif floors <= 1: return floors elif dp[eggs][floors] != -1: return dp[eggs][floors] l, r = 1, floors+1 while l < r: mid = l + (r-l) // 2 broken = search(eggs-1, mid-1) intact = search(eggs, floors-mid) if broken < intact: l = mid + 1 else: r = mid dp[eggs][floors] = 1 + search(eggs-1, l-1) return dp[eggs][floors] return search(eggs, floors)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: #https://leetcode.com/problems/super-egg-drop/discuss/159079/Python-DP-from-kn2-to-knlogn-to-kn def superEggDrop(self, K: int, N: int) -> int: dp=[[float('inf')]*(N+1) for _ in range(K+1)] for i in range(1, K+1): dp[i][0] = 0 dp[i][1] = 1 for j in range(1, N+1): dp[1][j] = j for i in range(2, K+1): k = 1 for j in range(2, N+1): while k < j+1 and dp[i][j-k] > dp[i-1][k-1]: k += 1 dp[i][j] = 1 + dp[i-1][k-1] return dp[K][N] # def superEggDrop(self, K: int, N: int) -> int: # dp = [[float('inf') for _ in range(N + 1)] for _ in range(K + 1)] # for i in range(1, K + 1): # dp[i][0] = 0 # dp[i][1] = 1 # for j in range(1, N + 1): # dp[1][j] = j # for i in range(2, K + 1): # for j in range(2, N + 1): # for k in range(1, j + 1): # dp[i][j] = min(dp[i][j], 1 + max(dp[i - 1][k - 1], dp[i][j - k])) # return dp[-1][-1]
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: memo = [[-1]*(K+1) for _ in range(N+1)] @lru_cache(None) def drop(egg, floor): if egg==1 or floor<=1: return floor if memo[floor][egg]!=-1: return memo[floor][egg] minAttempts = float('inf') l=1;h=floor while l<=h: mid = (l+h)//2 low = drop(egg-1, mid-1) high = drop(egg, floor-mid) attempts = 1 + max(low, high) if low<high: l=mid+1 else: h=mid-1 minAttempts = min(minAttempts, attempts) memo[floor][egg] = minAttempts return minAttempts return drop(K, N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: memo = {} def DP(k, n): if (k, n) not in memo: if n == 0: res = 0 elif k == 1: res = n else: l, r = 1, n while l < r: mid = (l + r) // 2 t1 = DP(k-1, mid-1) t2 = DP(k, n-mid) if t1 < t2: l = mid + 1 else: r = mid res = 1 + max(DP(k-1, l-1), DP(k, n-l)) memo[k, n] = res return memo[k, n] return DP(K, N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop1(self, K: int, N: int) -> int: # 超时!!! memo = [[2**31-1] * (N+1) for _ in range(K+1)] def dp(k, n): if k == 0: return 0 if k == 1: return n if n <= 1: return n if memo[k][n] < 2**31-1: return memo[k][n] ans = memo[k][n] for i in range(1, n+1): ans = min(ans, 1+max(dp(k-1, i-1), dp(k, n-i))) # 极小极大过程!!!alike memo[k][n] = ans return ans return dp(K, N) # D(K-1, i-1), monotonically increasing with i # D(K, N-i) is monotonically decreasing with i! # binary search! # D(K, N) = 1 + min(max(D(K-1, i-1), D(K, N-i))), 1<=i<=N # f(i) = D(K-1, i-1), f(i) is monotonically increasing with i # g(i) = D(K, N-i), g(i) is monotonically decreasing with i # we can use binary search to find i that minimizes max(f(i), g(i)) # # 一个函数单调增;一个函数单调减 # min (max (f, g)) # 就可以使用binary search # f(i) < g(i) 的时候,说明i太小了,需要增大i # f(i) > g(i) 的时候,说明i太大了,需要减少i # 我们需要的是first i, such that f(i) >= g(i) # time complexity = O(KNlogN) smaller than O(K*N*N) # space is same O(KN) def superEggDrop3(self, K: int, N: int) -> int: # 使用binary search,可以得到最好的结果了! memo = [[2**31-1] * (N+1) for _ in range(K+1)] def dp(k, n): if k == 0: return 0 if k == 1: return n if n <= 1: return n if memo[k][n] < 2**31-1: return memo[k][n] #ans = memo[k][n] #for i in range(1, n+1): # ans = min(ans, 1+max(dp(k-1, i-1), dp(k, n-i))) # 极小极大过程!!!alike left = 1 right = n + 1 res = 2**31-1 while left < right: mid = left + (right - left) // 2 broken = dp(k-1, mid-1) notbroken = dp(k, n-mid) if broken >= notbroken: # f(mid) >= g(mid) 最小的mid! right = mid res = min(res, broken+1) else: left = mid + 1 res = min(res, notbroken+1) memo[k][n] = res #1 + max(dp(k-1, left-1), dp(k, n-left)) return memo[k][n] return dp(K, N) def superEggDrop(self, K, N): amax = 2**31-1 memo = [[amax] * (N+1) for _ in range(K+1)] def dp(k, n): if k == 0: return 0 if k == 1: return n if n <= 1: return n # this time k>1 if memo[k][n] != amax: return memo[k][n] left = 1 right = n + 1 while left < right: mid = left + (right - left)//2 broken = dp(k-1, mid-1) notbroken = dp(k, n-mid) # mid+1 to N if broken >= notbroken: right = mid else: left = mid + 1 ans = 1 + max(dp(k-1, left-1), dp(k, n-left)) memo[k][n] = ans return ans return dp(K, N) def superEggDrop2(self, K: int, N: int) -> int: # 使用binary search,很好了! memo = [[2**31-1] * (N+1) for _ in range(K+1)] def dp(k, n): if k == 0: return 0 if k == 1: return n if n <= 1: return n if memo[k][n] < 2**31-1: return memo[k][n] #ans = memo[k][n] #for i in range(1, n+1): # ans = min(ans, 1+max(dp(k-1, i-1), dp(k, n-i))) # 极小极大过程!!!alike left = 1 right = n + 1 while left < right: mid = left + (right - left) // 2 # broken >= not_broken if dp(k-1, mid-1) >= dp(k, n-mid): # f(mid) >= g(mid) 最小的mid! right = mid else: left = mid + 1 # 通过binary search找到left是核心!找极值点! memo[k][n] = 1 + max(dp(k-1, left-1), dp(k, n-left)) return memo[k][n] return dp(K, N) def superEggDrop4(self, K, N): def f(x): ans = 0 r = 1 for i in range(1, K+1): r *= x-i+1 r //= i ans += r if ans >= N: break return ans # 还是使用了binary search! 速度很快啊!!! lo, hi = 1, N while lo < hi: mi = (lo + hi) // 2 if f(mi) < N: lo = mi + 1 else: hi = mi return lo
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: dp = [[0] * (K + 1) for i in range(N + 1)] for m in range(1, N + 1): for k in range(1, K + 1): dp[m][k] = dp[m - 1][k - 1] + dp[m - 1][k] + 1 if dp[m][K] >= N: return m return search(N, K)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: memo = dict() def dp(k, n): # base case if k == 1: return n if n == 0: return 0 if (k, n) in memo: return memo[(k, n)] res = float('inf') # for i in range(1, n + 1): # res = min(res, max(dp(k, n - i), dp(k - 1, i - 1)) + 1) # binary search res = float('inf') lo, hi = 1, n while lo <= hi: mid = lo + (hi - lo) // 2 broken = dp(k - 1, mid - 1) not_broken = dp(k, n - mid) if not_broken > broken: lo = mid + 1 res = min(res, not_broken + 1) else: hi = mid - 1 res = min(res, broken + 1) memo[(k, n)] = res return res return dp(K, N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, e: int, f: int) -> int: self.cache = [[-1]*(f+1) for _ in range(e+1)] return self.drop(e,f) def drop(self,e,f): if f == 0 or f == 1: return f if e == 1: return f if self.cache[e][f] != -1: return self.cache[e][f] ans = float('inf') l = 1 h = f while l <= h: mid = (l+h)//2 low = 0 high = 0 if self.cache[e-1][mid-1] != -1: low = self.cache[e-1][mid-1] else: low = self.drop(e-1,mid-1) if self.cache[e][f-mid] != -1: high = self.cache[e][f-mid] else: high = self.drop(e,f-mid) if low < high: l = mid + 1 else: h = mid -1 ans = min(max(low,high)+1,ans) # for k in range(1,f+1): # low = 0 # high = 0 # if self.cache[e-1][k-1] != -1: # low = self.cache[e-1][k-1] # else: # low = self.drop(e-1,k-1) # if self.cache[e][f-k] != -1: # high = self.cache[e][f-k] # else: # high = self.drop(e,f-k) # ans = min(max(low,high)+1,ans) self.cache[e][f] = ans return ans
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: memo = {} def dfs(k, n): if k == 1: # Only one egg, start from floor. return n if n <= 1: return n if (k, n) in memo: return memo[(k, n)] l, r = 0, n while l < r: mid = l + (r - l) // 2 left_cnt = dfs(k-1, mid-1) right_cnt = dfs(k, n-mid) if left_cnt < right_cnt: l = mid + 1 else: r = mid memo[(k, n)] = 1 + max(dfs(k-1, l-1), dfs(k, n-l)) return memo[(k, n)] return dfs(K, N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: # https://leetcode.com/problems/super-egg-drop/discuss/443089/Simplest-Python-DP-Solution-with-Detailed-Explanation-99Time-100Mem # M x K --> Given M moves and K eggs, what is the maximum floor we can check ? # M = 300 # big enough number # dp = [[0 for j in range(K+1)] for i in range(M+1)] # # Initialization 1 --> no move no floor --> dp[0][*] = 0 # # Initialization 2 --> no egg no floor --> dp[*][0] = 0 # # General case --> we want to find dp[m][k] --> we pick one egg and drop (1 move) # # --> now we have k or k-1 eggs, depending on whether the previous egg is broken # # --> so in either case, we can at least sum up 1 (first move) + dp[m-1][k] + dp[m-1][k-1] # for i in range(1, M+1): # for j in range(1, K+1): # dp[i][j] = 1 + dp[i-1][j] + dp[i-1][j-1] # if dp[i][j] >= N: # return i memo = dict() def dp(K, N): if K == 1: return N if N == 0: return 0 if (K, N) in memo: return memo[(K, N)] # res = float('inf') low, high = 1, N + 1 while low < high: mid = (low + high)//2 broken = dp(K-1, mid -1) notBroken = dp(K, N-mid) if broken >= notBroken: high = mid # res = min(broken + 1, res) else: low = mid + 1 # res = min(notBroken + 1, res) memo[(K,N)] = 1 + max(dp(K-1, low -1), dp(K, N-low)) return memo[(K,N)] return dp(K, N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: memo = {} def dp(k, n): if k <= 0: return 0 if k == 1: return n if n <= 1: return n if (k,n) in memo: return memo[(k,n)] l, r = 1, n+1 while l < r: mid = l + (r-l) //2 broken = dp(k - 1, mid - 1) unbroken = dp(k, n - mid) if broken >= unbroken: r = mid else: l = mid + 1 memo[(k,n)] = 1 + dp(k - 1, l - 1) return memo[(k,n)] return dp(K, N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: def dfs(k, n): if k == 1: return n if n <= 1: return n if (k, n) in memo: return memo[(k, n)] l, r = 0, n while l < r: mid = l + (r - l) // 2 left_num, right_num = dfs(k-1, mid-1), dfs(k, n-mid) if right_num > left_num: l = mid + 1 else: r = mid memo[(k, n)] = 1 + max(dfs(k-1, l-1), dfs(k, n-l)) return memo[(k, n)] memo = {} return dfs(K, N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
# More specifically, to get the k that best fits each drop, we don't need to go over all floors from 1 to j. # As for a fixed k, dp[i][k] goes up as k increases. This means dp[i-1][k-1] will increase and dp[i][j-k] will # decrease as k goes from 1 to j. The optimal value of k will be the middle point where the two meet. So to # get the optimal k value for dp[i][j], we can do a binary search for k from 1 to j. class Solution(object): def superEggDrop(self, K, N): def dfs(i, j): if i==1: return j if j==0: return 0 if j==1: return 1 if (i, j) in d: return d[i, j] lo, hi = 0, j while lo < hi: mid = (lo+hi)//2 left, right = dfs(i-1, mid-1), dfs(i, j-mid) # print(mid, left, right) if left < right: lo = mid + 1 else: hi = mid res = 1 + max(dfs(i-1, lo-1), dfs(i, j-lo)) d[i, j]=res return res d={} return dfs(K, N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: # dp[i] represents dp(1, i) dp = range(N + 1) for k in range(2, K + 1): # dp2 represents dp(k, i) dp2 = [0] A = 1 for n in range(1, N + 1): costA = max(dp[A - 1], dp2[n - A]) while A < n: B = A + 1 costB = max(dp[B - 1], dp2[n - B]) if costB > costA: break costA = costB A += 1 dp2.append(1 + costA) dp = dp2 return dp[-1]
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def eggDrop(self,floors,eggs,m): if floors<=1: return floors if eggs == 1: return floors if (floors,eggs) in m: return m[(floors,eggs)] l,h = 0,floors while l<h: mid = (l+h)>>1 kelge,mele = self.eggDrop(mid-1,eggs-1,m) , self.eggDrop(floors-mid,eggs,m) if kelge>=mele: h = mid else: l = mid+1 drops = 1 + max(self.eggDrop(l-1,eggs-1,m),self.eggDrop(floors-l,eggs,m)) m[(floors,eggs)] = drops return drops def superEggDrop(self, K: int, N: int) -> int: return self.eggDrop(N,K,{})
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, eggs: int, floors: int) -> int: building = [[0] * (eggs+1) for _ in range(floors+1)] currFloor = 0 while building[currFloor][eggs] < floors: currFloor += 1 for i in range(1 , eggs+1): building[currFloor][i] = building[currFloor-1][i-1] + building[currFloor-1][i] + 1 return currFloor
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: dp = {} def num_moves(n, k): if (n, k) not in dp: if n == 0: ans = 0 elif k == 1: ans = n else: left = 1 right = n + 1 while left < right: floor = (left + right) // 2 safe_moves = num_moves(n - floor, k) unsafe_moves = num_moves(floor - 1, k - 1) if safe_moves > unsafe_moves: left = floor + 1 else: right = floor floor = left safe_moves = num_moves(n - floor, k) unsafe_moves = num_moves(floor - 1, k - 1) nk_moves = max(safe_moves, unsafe_moves) + 1 if floor > 1: prev_floor = left - 1 prev_safe_moves = num_moves(n - prev_floor, k) prev_unsafe_moves = num_moves(prev_floor - 1, k - 1) prev_nk_moves = max(prev_safe_moves, prev_unsafe_moves) + 1 nk_moves = min(nk_moves, prev_nk_moves) ans = nk_moves dp[(n, k)] = ans return dp[(n, k)] return num_moves(N, K)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: dp = {} def num_moves(n, k): if (n, k) not in dp: if n == 0: ans = 0 elif k == 1: ans = n else: left = 1 right = n + 1 while left < right: floor = (left + right) // 2 safe_moves = num_moves(n - floor, k) unsafe_moves = num_moves(floor - 1, k - 1) if safe_moves > unsafe_moves: left = floor + 1 else: right = floor floor = left safe_moves = num_moves(n - floor, k) unsafe_moves = num_moves(floor - 1, k - 1) nk_moves = max(safe_moves, unsafe_moves) + 1 if floor > 1: prev_floor = left - 1 prev_safe_moves = num_moves(n - prev_floor, k) prev_unsafe_moves = num_moves(prev_floor - 1, k - 1) prev_nk_moves = max(prev_safe_moves, prev_unsafe_moves) + 1 nk_moves = min(nk_moves, prev_nk_moves) ans = nk_moves dp[(n, k)] = ans return dp[(n, k)] return num_moves(N, K)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: memo = dict() def dp(K, N): if (K, N) in memo: return memo[(K,N)] if K == 1: return N if N == 0: return 0 res = sys.maxsize # for i in range(1, N + 1): # res = min(res, max(dp(K - 1, i - 1), dp(K, N - i)) + 1) lo, hi = 1, N while lo <= hi: mid = int(lo + (hi - lo) / 2) broken = dp(K - 1, mid - 1) not_broken = dp(K, N - mid) if broken < not_broken: lo = mid + 1 res = min(not_broken + 1, res) else: hi = mid - 1 res = min(broken + 1, res) memo[(K, N)] = res return res return dp(K, N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: def dfs(e, f): if e == 1: return f if f == 0 or f == 1: return f if (e, f) in cache: return cache[(e, f)] left, right = 1, f + 1 while left < right: mid = left + (right - left) // 2 i, j = dfs(e - 1, mid - 1), dfs(e, f - mid) if i < j: left = mid + 1 else: right = mid cache[(e, f)] = 1 + max(dfs(e - 1, left - 1), dfs(e, f - left)) return cache[(e, f)] cache = dict() return dfs(K, N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: results = [[None for _ in range(N+1)] for _ in range(K+1)] def dp(K, N): if K == 1: return N if N == 0: return 0 if results[K][N]: return results[K][N] res = N left = 1 right = N while left <= right: mid = int((right+left)/2) broken_num = dp(K-1, mid-1) notbroken_num = dp(K, N-mid) if broken_num > notbroken_num: right = mid - 1 res = min(res, broken_num + 1) else: left = mid + 1 res = min(res, notbroken_num + 1) results[K][N] = res # for i in range(1, N+1): # results[K][N] = min(results[K][N], max(dp(K-1, i-1), dp(K, N-i)) + 1) # return results[K][N] return res # dp(K, N) # print(results) return dp(K, N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: memo = [[-1]*(K+1) for _ in range(N+1)] def drop(egg, floor): if egg==1 or floor<=1: return floor if memo[floor][egg]!=-1: return memo[floor][egg] minAttempts = float('inf') l=1;h=floor while l<=h: mid = (l+h)//2 low = drop(egg-1, mid-1) high = drop(egg, floor-mid) attempts = 1 + max(low, high) if low<high: l=mid+1 else: h=mid-1 minAttempts = min(minAttempts, attempts) memo[floor][egg] = minAttempts return minAttempts return drop(K, N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: dp = [[0]*(N+1) for _ in range(K+1)] for i in range(1, N+1): dp[1][i] = i for i in range(2, K+1): s = 1 for j in range(1, N+1): dp[i][j] = j while s<j and dp[i][j-s]>dp[i-1][s-1]: s += 1 dp[i][j] = min(dp[i][j], max(dp[i][j-s], dp[i-1][s-1])+1) return dp[K][N]
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: num_moves = [n for n in range(N + 1)] for k in range(2, K + 1): next_num_moves = [0 for _ in range(N + 1)] floor = 1 for n in range(1, N + 1): safe_moves = next_num_moves[n - floor] unsafe_moves = num_moves[floor - 1] nk_moves = 1 + max(safe_moves, unsafe_moves) while floor < N: next_floor = floor + 1 next_safe_moves = next_num_moves[n - next_floor] next_unsafe_moves = num_moves[next_floor - 1] next_nk_moves = 1 + max(next_safe_moves, next_unsafe_moves) if next_nk_moves < nk_moves: floor = next_floor nk_moves = next_nk_moves else: break next_num_moves[n] = nk_moves num_moves = next_num_moves return num_moves[N]
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
from math import inf from typing import List class Solution: def superEggDrop(self, K: int, N: int) -> int: self.memo = {} return self.rec(K, N) def rec(self, i, j): if i == 1: return j if j == 0 or j == 1: return j if (i, j) in self.memo: return self.memo[i, j] lo, hi = 0, j while lo < hi: mid = lo + (hi - lo) // 2 left, right = self.rec(i - 1, mid - 1), self.rec( i, j - mid) if left < right: lo = mid + 1 else: hi = mid res = 1 + self.max(self.rec(i - 1, lo - 1), self.rec(i, j - lo)) self.memo[i, j] = res return res def dp(self, K: int, N: int) -> int: #dp[i][j] - i eggs for j floors dp = [[inf for _ in range(N + 1)] for _ in range(K + 1)] #dp[i][0] -> 0, dp[1][j] -> j, dp[i][1] -> 1 for i in range(K + 1): dp[i][0] = 0 dp[i][1] = 1 for j in range(N + 1): dp[1][j] = j for i in range(2, K + 1): for j in range(2, N + 1): left, right = 0, j while left < right: mid = left + (right - left) // 2 if dp[i - 1][mid - 1] < dp[i][j - mid]: left = mid + 1 else: right = mid dp[i][j] = 1 + self.max(dp[i - 1][left - 1], dp[i][j - left]) #for k in range(i, j + 1): # dp[i][j] = self.min(dp[i][j] , 1 + self.max(dp[i - 1][k - 1], dp[i][j - k]) return dp[K][N] def min(self, a, b): return a if a < b else b def max(self, a, b): return a if a > b else b
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: table = dict() def dp(K: int, N: int) -> int: if (K == 1) : return N if (N <= 0) : return 0 if (K, N) in table: return table[(K, N)]; lo = 1 hi = N while (lo <= hi): mid = lo + (hi - lo) // 2 up = dp(K, N - mid) dn = dp(K-1, mid-1) if (up >= dn): lo = mid + 1 else: hi = mid - 1 table[(K, N)] = 1 + min(dp(K, N - hi), dp(K - 1, lo - 1)) return table[(K, N)] return dp(K, N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: memo = [[0] * (K + 1) for _ in range(N + 1)] for m in range(1, N + 1): for k in range(1, K + 1): memo[m][k] = memo[m - 1][k - 1] + 1 + memo[m - 1][k] if memo[m][k] >= N: return m
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: def dp(k, n): if k == 1: return n if n == 0: return 0 if (k, n) in memo: return memo[(k, n)] res = sys.maxsize lo, hi = 1, n while lo <= hi: mid = (lo + hi) // 2 broken = dp(k - 1, mid - 1) safe = dp(k, n - mid) if broken > safe: hi = mid - 1 res = min(res, broken + 1) else: lo = mid + 1 res = min(res, safe + 1) memo[(k, n)] = res return res memo = {} return dp(K, N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: def dp(k, n): if k == 1: return n if n == 0: return 0 if (k, n) in memo: return memo[(k, n)] res = sys.maxsize lo, hi = 1, n while lo <= hi: mid = (lo + hi) // 2 broken = dp(k - 1, mid - 1) # 碎 not_broken = dp(k, n - mid) # 没碎 # res = min(max(碎,没碎) + 1) if broken > not_broken: hi = mid - 1 res = min(res, broken + 1) else: lo = mid + 1 res = min(res, not_broken + 1) memo[(k, n)] = res return res memo = {} return dp(K, N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: cache = {} def drop(egg, floor): if egg==1 or floor<=1: return floor if (egg, floor) in cache: return cache[(egg, floor)] minAttempts = float('inf') l=1;h=floor while l<=h: mid = (l+h)//2 low = drop(egg-1, mid-1) high = drop(egg, floor-mid) attempts = 1 + max(low, high) if low<high: l=mid+1 else: h=mid-1 minAttempts = min(minAttempts, attempts) cache[(egg, floor)] = minAttempts return minAttempts return drop(K, N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution(object): def superEggDrop(self, K, N): memo = {} def dp(K, N): if K == 1: return N if N == 0: return 0 if (K, N) in memo: return memo[(K, N)] 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) memo[(K, N)] = res return res return dp(K, N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: table = dict() def dp(K: int, N: int) -> int: if (K == 1) : return N if (N <= 0) : return 0 if (K, N) in table: return table[(K, N)]; lo, hi = 1, N res = N while (lo <= hi): mid = lo + (hi - lo) // 2 up = dp(K, N - mid) dn = dp(K-1, mid-1) if (up >= dn): lo = mid + 1 res = min(res, 1 + up) else: hi = mid - 1 res = min(res, 1 + dn) table[(K, N)] = res return res return dp(K, N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: memo = {} def dp(K, N): if K == 1: return N if N == 0: return 0 if (K, N) in memo: return memo[(K,N)] 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) # for i in range(1, N+1): # res = min(res, max(dp(K-1, i -1), dp(K, N - i)) + 1) memo[(K,N)] = res return res return dp(K, N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: # dp[K][N] = min_X(max(dp[K-1][X-1], dp[K][N-X])) dp = [[sys.maxsize for i in range(N+1)] for j in range(K+1)] def DP(k, n): if k < 0 or n < 0: return 0 if k == 0: return 0 if k == 1: return n if n == 0: return 0 if n == 1: return 1 if dp[k][n] != sys.maxsize: return dp[k][n] # find the minimal dp[k][n] l = 0 r = n while (l < r): m = (l+r) // 2 if DP(k-1, m-1) >= DP(k, n-m): r = m else: l = m+1 dp[k][n] = 1+max(DP(k-1, l-1), DP(k, n-l)) return dp[k][n] return DP(K, N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: # K=1, moves=N # K>=2, moves=max(dfs(K-1, i - 1), dfs(K, N - i)) def dfs(k, n, memo): if k == 1: return n if n == 0: return 0 if (k, n) in memo: return memo[(k, n)] res = float('inf') # for i in range(1, n+1): # res = min(res, max(dfs(k-1, i-1, memo), dfs(k, n-i, memo)) + 1) lo, hi = 1, n while lo <= hi: mid = (lo + hi) // 2 broken = dfs(k-1, mid-1, memo) no_broken = dfs(k, n-mid, memo) if broken > no_broken: hi = mid - 1 res = min(res, broken + 1) else: lo = mid + 1 res = min(res, no_broken + 1) memo[(k, n)] = res return res return dfs(K, N, {})
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: memo = {} def dp(k, n): if (k, n) not in memo: if n == 0: ans = 0 elif k == 1: ans = n else: lo, hi = 1, n # keep a gap of 2 X values to manually check later while lo < hi: x = (lo + hi) // 2 t1 = dp(k-1, x-1) t2 = dp(k, n-x) if t1 < t2: lo = x + 1 else: hi = x ans = 1 + min(max(dp(k-1, x-1), dp(k, n-x)) for x in (lo, hi)) memo[k, n] = ans return memo[k, n] return dp(K, N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: def f(x): ans = 0 r = 1 for i in range(1, K+1): r *= (x-i+1)/i #r //= i ans += r if ans >= N: break return ans low, high = 1, N while low < high: middle = (low + high) // 2 if f(middle) < N: low = middle + 1 else: high = middle return low
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: # # brutal force - time O(kn^2); space O(kn) # dp = [[float('inf')] * (N+1) for _ in range(K+1)] # for egg_num in range(1, K+1): # dp[egg_num][0] = 0 # dp[egg_num][1] = 1 # for floor_num in range(1, N+1): # dp[1][floor_num] = floor_num # for egg_num in range(2, K+1): # for floor_num in range(2, N+1): # for start_floor in range(1, floor_num+1): # dp[egg_num][floor_num] = min(dp[egg_num][floor_num], 1+max(dp[egg_num-1][start_floor-1], dp[egg_num][floor_num-start_floor])) # print(dp) # return dp[K][N] # time - O(nk) for declare and O(klogn) for running; space O(nk) dp = [[0] * (K+1) for _ in range(N+1)] for m in range(1, N+1): for k in range(1, K+1): dp[m][k] = dp[m-1][k-1] + dp[m-1][k] + 1 if dp[m][K] >= N: return m
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: dp_dict = dict() def dp(K, N): if K == 1: return N if N == 0: return 0 if (K, N) in dp_dict: return dp_dict[(K, N)] ans = inf # # normal # for i in range(1, N+1): # ans = min(ans, max(dp(K-1, i-1), dp(K,N-i))+1) # dp_dict[(K, N)] = ans # return ans # binary low, high = 1, N while low <= high: mid = (high + low) // 2 broken = dp(K-1, mid-1) good = dp(K, N-mid) if broken > good: high = mid - 1 ans = min(broken+1, ans) else: low = mid + 1 ans = min(good+1, ans) dp_dict[(K, N)] = ans return ans return dp(K, N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: # https://leetcode.com/problems/super-egg-drop/discuss/443089/Simplest-Python-DP-Solution-with-Detailed-Explanation-99Time-100Mem # M x K --> Given M moves and K eggs, what is the maximum floor we can check ? # M = 300 # big enough number # dp = [[0 for j in range(K+1)] for i in range(M+1)] # # Initialization 1 --> no move no floor --> dp[0][*] = 0 # # Initialization 2 --> no egg no floor --> dp[*][0] = 0 # # General case --> we want to find dp[m][k] --> we pick one egg and drop (1 move) # # --> now we have k or k-1 eggs, depending on whether the previous egg is broken # # --> so in either case, we can at least sum up 1 (first move) + dp[m-1][k] + dp[m-1][k-1] # for i in range(1, M+1): # for j in range(1, K+1): # dp[i][j] = 1 + dp[i-1][j] + dp[i-1][j-1] # if dp[i][j] >= N: # return i memo = dict() def dp(K, N): if K == 1: return N if N == 0: return 0 if (K, N) in memo: return memo[(K, N)] res = float('inf') low, high = 1, N+1 while low < high: mid = (low + high)//2 broken = dp(K-1, mid -1) notBroken = dp(K, N-mid) if broken > notBroken: high = mid res = min(broken + 1, res) else: low = mid + 1 res = min(notBroken + 1, res) # for i in range(1, N+1): # res = min(res, \\ # max(dp(K-1, i-1), dp(K, N-i))+ 1) memo[(K,N)] = res return res return dp(K, N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: DP = range(N + 1) for k in range(2, K + 1): DP2 = [0] x = 1 for n in range(1, N+1): while x < n and max(DP[x-1], DP2[n - x]) > max(DP[x], DP2[n - x - 1]): x += 1 DP2.append(1 + max(DP[x-1], DP2[n - x])) DP = DP2 return DP[-1]
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: memo={} def dp(K,N): if K==1: return N if N==0: return 0 if (K,N) in memo: return memo[(K,N)] res=float('inf') l,h=1,N while l<=h: mid=(l+h)//2 broken=dp(K-1,mid-1) not_broken=dp(K,N-mid) if broken>not_broken: h=mid-1 res=min(res,1+broken) else: l=mid+1 res=min(res,1+not_broken) memo[(K,N)]=res return res return dp(K,N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: memo = {} def dp(K, N): if K == 1: return N if N == 0: return 0 if (K, N) in memo: return memo[(K, N)] res = float('inf') lo, hi = 1, N while lo <= hi: mid = lo + (hi - lo) // 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) # for i in range(1, N+1): # res = min(res, # max( # dp(K, N-i), # dp(K-1, i-1) # ) + 1 # ) memo[(K, N)] = res return res return dp(K, N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
# K and N is suitation # choice: need to choose drop egg on which floor # use binary search to optimize complexity class Solution: def superEggDrop(self, K: int, N: int) -> int: memo = {} def dp(K: int, N: int): # base case if N == 0: return 0 if K == 1: return N if (K, N) in memo: return memo[(K, N)] 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) memo[(K, N)] = res return res return dp(K, N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: memo={} def dp(k, n): if n==0: return 0 if k==1: return n if (k,n) in memo: return memo[(k,n)] res=float('inf') lo=1 hi=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) # for i in range(1,n+1): # res=min(res,max(dp(k-1,i-1), dp(k,n-i))+1) memo[(k,n)]=res return res return dp(K,N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: memo = dict() def dp(K, N): if K == 1: return N if N == 0: return 0 if (K, N) in memo: return memo[(K, N)] # for 1 <= i <= N: # res = min(res, # max( # dp(K - 1, i - 1), # dp(K, N - i) # ) + 1 # ) 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) # 没碎 # res = min(max(碎,没碎) + 1) if broken > not_broken: hi = mid - 1 res = min(res, broken + 1) else: lo = mid + 1 res = min(res, not_broken + 1) memo[(K, N)] = res return res return dp(K, N)
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: t = [[0]*(K+1) for _ in range(N+1)] for i in range(1,N+1): for j in range(1,K+1): t[i][j] = t[i-1][j] + t[i-1][j-1] + 1 if t[i][j] >= N: return i
You are given K eggs, and you have access to a building with N floors from 1 to N.  Each egg is identical in function, and if an egg breaks, you cannot drop it again. You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).  Your goal is to know with certainty what the value of F is. What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?   Example 1: Input: K = 1, N = 2 Output: 2 Explanation: Drop the egg from floor 1. If it breaks, we know with certainty that F = 0. Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1. If it didn't break, then we know with certainty F = 2. Hence, we needed 2 moves in the worst case to know what F is with certainty. Example 2: Input: K = 2, N = 6 Output: 3 Example 3: Input: K = 3, N = 14 Output: 4   Note: 1 <= K <= 100 1 <= N <= 10000
class Solution: def superEggDrop(self, K: int, N: int) -> int: memo={} def dp(k,n): if k==1: return n if n==0: return 0 if (k,n) in memo: return memo[(k,n)] 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) # 没碎 # res = min(max(碎, 没碎) + 1) if broken > not_broken: hi = mid - 1 res = min(res, broken + 1) else: lo = mid + 1 res = min(res, not_broken + 1) memo[(k,n)]=res return res return dp(K,N)