inputs
stringlengths 50
14k
| targets
stringlengths 4
655k
|
|---|---|
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
while low <= high:
mid = (low + high)//2
broken = dp(K-1, mid -1)
notBroken = dp(K, N-mid)
if broken > notBroken:
high = mid -1
res = min(broken + 1, res)
else:
low = mid + 1
res = min(notBroken + 1, res)
# 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:
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')
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 = 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')
# 用二分搜索代替线性搜索
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:
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
|
# 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)]
lo = 1
hi = n
res = float('inf')
# we use binary search to speedup the process
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)
# write into memo
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')
# 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 = lo + (hi - lo)//2
broken = dp(K-1, mid-1)
unbroken = dp(K, N - mid)
if broken > unbroken:
hi = mid - 1
res = min(res, broken+1)
else:
lo = mid + 1
res = min(res, unbroken+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 = {(1, n) : n for n in range(1, N + 1)}
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] = max(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 = [[None] * K for _ in range(N+1)]
for j in range(K):
dp[0][j] = 0
for i in range(1, N+1):
dp[i][0] = i
for j in range(1, K):
dp[1][j] = 1
m = 1
for i in range(2, N+1):
while m < i and dp[i-m][j] >= dp[m-1][j-1] and dp[i-m-1][j] >= dp[m][j-1]:
m += 1
dp[i][j] = max(dp[i-m][j], dp[m-1][j-1]) + 1
# for m in range(1, i+1):
# dp[i][j] = min(dp[i][j], max(dp[m-1][j-1], dp[i-m][j]) + 1)
return dp[N][K-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 = dict()
return self.dp(K, N, memo)
def dp(self, 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(self.dp(k-1, i-1, memo), self.dp(k, n-i, memo)) + 1 )
'''
lo, hi = 1, n
while lo <= hi:
mid = lo + (hi - lo)//2
broken = self.dp(k-1, mid-1, memo)
not_broken = self.dp(k, n-mid, memo)
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
|
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:
# base case for only 1 egg
dp=list(range(N+1))
for k in range(1,K):
newdp=[0]
opt=1
for n in range(1,N+1):
# the optimum choice (intersection of two values) increases with n
tmp=max(dp[opt-1],newdp[n-opt])
while opt<n and tmp>max(dp[opt],newdp[n-opt-1]):
tmp=max(dp[opt],newdp[n-opt-1])
opt+=1
newdp.append(1+tmp)
dp=newdp
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:
cache = {}
return self.drop_iter(K, N, cache)
def drop_iter(self, K, N, cache):
if K == 1:
return N
if N == 0:
return 0
if (K, N) in cache:
return cache[(K, N)]
res = float('inf')
# for i in range(1, N+1):
# temp = max(self.drop_iter(K, N-i, cache), self.drop_iter(K-1, i-1, cache)) + 1
# res = min(res, temp)
left = 1
right = N
# self.drop_iter(K, N-i, cache) 单调减
# self.drop_iter(K-1, i-1, cache) 单调增
while left <= right:
mid = (left + right) // 2
borke = self.drop_iter(K - 1, mid - 1, cache)
not_broke = self.drop_iter(K, N - mid, cache)
if borke > not_broke:
right = mid - 1
res = min(borke + 1, res)
else:
left = mid + 1
res = min(not_broke + 1, res)
cache[(K, N)] = res
return res
|
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:
dp = [[0] * (K+1) for n 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:
m = [[0 for i in range(N + 1)] for j in range(K + 1)]
def find(k, n):
if k == 1:
return n
if n == 0:
return 0
if m[k][n] > 0:
return m[k][n]
def bsearch():
st, end = 1, n
while st <= end:
m = (st + end) // 2
if predicate(m):
end = m - 1
else:
st = m + 1
return st, end
def predicate(x):
return True if find(k-1, x - 1) >= find(k, n - x) else False
st, end = bsearch()
v_st = max(find(k - 1,st - 1), find(k, n - st)) if st <= N else math.inf
v_end = max(find(k - 1, end - 1), find(k, n - end)) if end >= 1 else math.inf
m[k][n] = min(v_st, v_end) + 1
return m[k][n]
return find(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:
cachedSolutions = [[-1 for i in range(N+1)] for j in range(K+1)]
return self.getMinDrops(K, N, cachedSolutions)
def getMinDrops(self, eggs, floors, cachedSolutions):
if cachedSolutions[eggs][floors] > 0:
return cachedSolutions[eggs][floors]
elif eggs == 0:
cachedSolutions[eggs][floors] = 0
return 0
elif eggs == 1 or floors <= 2:
cachedSolutions[eggs][floors] = floors
return floors
else:
## Arbitrarily large number to initialize the minimum number of drops.
smallestIndex = 1
largestIndex = floors
minDrops = 100000000
while(smallestIndex<=largestIndex):
## We want to find the minimum of the maximums of all possible subproblems and do it through binary search.
middleIndex = round(smallestIndex+(largestIndex-smallestIndex)/2)
## Solve the subproblems.
minDropsA = self.getMinDrops(eggs-1, middleIndex-1, cachedSolutions)
minDropsB = self.getMinDrops(eggs, floors-middleIndex, cachedSolutions)
## Find a new minimum number of drops for validating that all floors are a solution or not.
tempMinDrops = max(minDropsA, minDropsB) + 1
minDrops = min(minDrops, tempMinDrops)
## Check which half will contain the valid solution.
if (minDropsA > minDropsB):
largestIndex = middleIndex - 1
else:
smallestIndex = middleIndex + 1
cachedSolutions[eggs][floors] = minDrops
return cachedSolutions[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:
def superEggDrop(self, K: int, N: int) -> int:
m = [[0 for i in range(N + 1)] for j in range(K + 1)]
def find(k, n):
if k == 1:
return n
if n == 0:
return 0
if m[k][n] > 0:
return m[k][n]
def bsearch():
st, end = 1, n
while st <= end:
m = (st + end) // 2
if predicate(m):
end = m - 1
else:
st = m + 1
return st, end
def predicate(x):
return True if find(k-1, x - 1) >= find(k, n - x) else False
st, end = bsearch()
if st > N:
v_st = math.inf
v_end = 1 + max(find(k - 1, end - 1), find(k, n - end))
elif end < 1:
v_end = math.inf
v_st = 1 + max(find(k - 1,st - 1), find(k, n - st))
else:
v_st = 1 + max(find(k - 1,st - 1), find(k, n - st))
v_end = 1 + max(find(k - 1, end - 1), find(k, n - end))
m[k][n] = min(v_st, v_end)
return m[k][n]
return find(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] * (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
|
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 _ in range(N+1)]
for move in range(1, N+1):
for egg in range(1, K+1):
dp[move][egg] = dp[move - 1][egg - 1] + dp[move - 1][egg] + 1
if dp[move][K] >= 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:
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
|
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 segment_sum(x,k):
# # x is worst case, k is number of eggs available
# # returns max N for worst case x, eggs k
# if k==1:
# return x
# return sum(segment_sum(j, k-1)+1 for j in range(x))
# worst_case=0
# while segment_sum(worst_case, K) < N:
# worst_case+=1
# return worst_case
# class Solution(object):
# def superEggDrop(self, K, N):
# 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
# class Solution(object):
# def superEggDrop(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
# lo, hi = 1, N
# while lo < hi:
# mi = (lo + hi) // 2
# if f(mi) < N:
# lo = mi + 1
# else:
# hi = mi
# return lo
from scipy.special import comb, factorial, hyp2f1
from math import log2, floor
def h(k,n):
return round(2**n-1-comb(n,k+1)*hyp2f1(1,k-n+1,k+2,-1))
def h2(k,n):
return sum(comb(n,j) for j in range(1,k+1))
def fast_sum(k,l):
s = 0
c = 1
for i in range(1,k+1):
c *= (l-i+1)/(i)
s += c
return s
def e(k, n):
if k == 1:
return n
return sum(e(k-1, j)+1 for j in range(n))
def f(K, x):
ans = 0
r = 1
for i in range(1, K+1):
r *= x-i+1
r //= i
ans += r
return ans
class Solution:
def superEggDrop(self, k: int, n: int) -> int:
# best_case = floor(log2(n)+1)
# if k >= best_case:
# return best_case
if k == 0:
return n
a, b = 1, n
while a < b:
c = (a+b)//2
if fast_sum(k,c) < n:
a = c + 1
else:
b = c
return a
|
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 segment_sum(x,k):
# # x is worst case, k is number of eggs available
# # returns max N for worst case x, eggs k
# if k==1:
# return x
# return sum(segment_sum(j, k-1)+1 for j in range(x))
# worst_case=0
# while segment_sum(worst_case, K) < N:
# worst_case+=1
# return worst_case
# class Solution(object):
# def superEggDrop(self, K, N):
# 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
# class Solution(object):
# def superEggDrop(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
# lo, hi = 1, N
# while lo < hi:
# mi = (lo + hi) // 2
# if f(mi) < N:
# lo = mi + 1
# else:
# hi = mi
# return lo
from scipy.special import comb, factorial, hyp2f1
from math import log2, floor
def h(k,n):
return round(2**n-1-comb(n,k+1)*hyp2f1(1,k-n+1,k+2,-1))
def h2(k,n):
return sum(comb(n,j) for j in range(1,k+1))
def fast_sum(k,l):
s = 0
c = 1
for i in range(1,k+1):
c *= (l-i+1)/(i)
s += c
return s
def e(k, n):
if k == 1:
return n
return sum(e(k-1, j)+1 for j in range(n))
def f(K, x):
ans = 0
r = 1
for i in range(1, K+1):
r *= x-i+1
r //= i
ans += r
return ans
class Solution:
def superEggDrop(self, k: int, n: int) -> int:
# best_case = floor(log2(n)+1)
# if k >= best_case:
# return best_case
if k == 0:
return n
l = 0
while fast_sum(k, l) < n:
l+=1
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:
# 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)
dp = [[0 for _ in range(N+1)] for _ in range(K+1)]
m = 0
while dp[K][m] < N:
m += 1
for k in range(1, K+1):
dp[k][m] = dp[k-1][m-1] + dp[k][m-1] + 1
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, N):
dp = [0, 0]
m = 0
while dp[-1] < N:
for i in range(len(dp) - 1, 0, - 1):
dp[i] += dp[i - 1] + 1
if len(dp) < K + 1:
dp.append(dp[-1])
m += 1
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 segment_sum(x,k):
# # x is worst case, k is number of eggs available
# # returns max N for worst case x, eggs k
# if k==1:
# return x
# return sum(segment_sum(j, k-1)+1 for j in range(x))
# worst_case=0
# while segment_sum(worst_case, K) < N:
# worst_case+=1
# return worst_case
# class Solution(object):
# def superEggDrop(self, K, N):
# 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
# class Solution(object):
# def superEggDrop(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
# lo, hi = 1, N
# while lo < hi:
# mi = (lo + hi) // 2
# if f(mi) < N:
# lo = mi + 1
# else:
# hi = mi
# return lo
from scipy.special import comb, factorial, hyp2f1
from math import log2, floor
def h(k,n):
return round(2**n-1-comb(n,k+1)*hyp2f1(1,k-n+1,k+2,-1))
def h2(k,n):
return sum(comb(n,j) for j in range(1,k+1))
def fast_sum(k,n):
s = 0
c = 1
for i in range(1,k+1):
c *= (n-i+1)/(i)
s += c
return s
def e(k, n):
if k == 1:
return n
return sum(e(k-1, j)+1 for j in range(n))
def f(K, x):
ans = 0
r = 1
for i in range(1, K+1):
r *= x-i+1
r //= i
ans += r
return ans
class Solution:
def superEggDrop(self, k: int, n: int) -> int:
# best_case = floor(log2(n)+1)
# if k >= best_case:
# return best_case
if k == 0:
return n
l = 0
while fast_sum(k, l) < n:
l+=1
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 segment_sum(x,k):
# # x is worst case, k is number of eggs available
# # returns max N for worst case x, eggs k
# if k==1:
# return x
# return sum(segment_sum(j, k-1)+1 for j in range(x))
# worst_case=0
# while segment_sum(worst_case, K) < N:
# worst_case+=1
# return worst_case
# class Solution(object):
# def superEggDrop(self, K, N):
# 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
# class Solution(object):
# def superEggDrop(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
# lo, hi = 1, N
# while lo < hi:
# mi = (lo + hi) // 2
# if f(mi) < N:
# lo = mi + 1
# else:
# hi = mi
# return lo
from scipy.special import comb, factorial, hyp2f1
from math import log2, floor
def h(k,n):
return round(2**n-1-comb(n,k+1)*hyp2f1(1,k-n+1,k+2,-1))
def h2(k,n):
return sum(comb(n,j) for j in range(1,k+1))
def fast_sum(k,l,n):
s = 0
c = 1
for i in range(1,k+1):
c *= (l-i+1)/(i)
s += c
if s >= n:
return s
return s
def e(k, n):
if k == 1:
return n
return sum(e(k-1, j)+1 for j in range(n))
def f(K, x):
ans = 0
r = 1
for i in range(1, K+1):
r *= x-i+1
r //= i
ans += r
return ans
class Solution:
def superEggDrop(self, k: int, n: int) -> int:
# best_case = floor(log2(n)+1)
# if k >= best_case:
# return best_case
if k == 0:
return n
l = 0
while fast_sum(k, l, n) < n:
l+=1
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:
# dp[k][m]: given k eggs, max move m, the highest floor can find F
# dp[k][m] = dp[k - 1][m - 1] + dp[k][m - 1] + 1
# max m will be N, linear scan
dp = [[0 for i in range(N + 1)] for j in range(K + 1)]
m = 0
while dp[K][m] < N:
m += 1
for k in range(1, K + 1):
dp[k][m] = dp[k - 1][m - 1] + dp[k][m - 1] + 1
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 segment_sum(x,k):
# # x is worst case, k is number of eggs available
# # returns max N for worst case x, eggs k
# if k==1:
# return x
# return sum(segment_sum(j, k-1)+1 for j in range(x))
# worst_case=0
# while segment_sum(worst_case, K) < N:
# worst_case+=1
# return worst_case
# class Solution(object):
# def superEggDrop(self, K, N):
# 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
# class Solution(object):
# def superEggDrop(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
# lo, hi = 1, N
# while lo < hi:
# mi = (lo + hi) // 2
# if f(mi) < N:
# lo = mi + 1
# else:
# hi = mi
# return lo
from scipy.special import comb, factorial, hyp2f1
from math import log2, floor
def h(k,n):
return round(2**n-1-comb(n,k+1)*hyp2f1(1,k-n+1,k+2,-1))
def h2(k,n):
return sum(comb(n,j) for j in range(1,k+1))
def fast_sum(k,l):
s = 0
c = 1
for i in range(1,k+1):
c *= (l-i+1)/(i)
s += c
return s
def e(k, n):
if k == 1:
return n
return sum(e(k-1, j)+1 for j in range(n))
def f(K, x):
ans = 0
r = 1
for i in range(1, K+1):
r *= x-i+1
r //= i
ans += r
return ans
class Solution:
def superEggDrop(self, k: int, n: int) -> int:
# best_case = floor(log2(n)+1)
# if k >= best_case:
# return best_case
if k == 0:
return n
a, b = 1, n
while a < b:
c = (a+b)//2
if h2(k,c) < n:
a = c + 1
else:
b = c
return a
|
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][m] means the number floors can be identify when using k egges with maximum try m times
dp = [[0 for col in range(N + 2)] for row in range(K + 2)]
m = 0
while dp[K][m] < N:
m += 1
for k in range(1, K + 1):
dp[k][m] = dp[k - 1][m - 1] + dp[k][m - 1] + 1
return m
# 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:
# dpDict = {}
# def dp(K,N):
# if K == 1: return N
# if N == 0: return 0
# if (K, N) in dpDict: return dpDict[K,N]
# res = float('inf')
# for i in range(1, N + 1):
# res = min(res,
# 1 + max(dp(K-1, i-1), dp(K,N-i))
# )
# dpDict[K,N] = res
# return res
# return dp(K,N)
dp = [[0 for _ in range(N+1)] for _ in range(K+1)]
m = 0
while dp[K][m] < N:
m += 1
for i in range(1,K+1):
dp[i][m] = dp[i][m-1] + dp[i-1][m-1] + 1
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 segment_sum(x,k):
# # x is worst case, k is number of eggs available
# # returns max N for worst case x, eggs k
# if k==1:
# return x
# return sum(segment_sum(j, k-1)+1 for j in range(x))
# worst_case=0
# while segment_sum(worst_case, K) < N:
# worst_case+=1
# return worst_case
# class Solution(object):
# def superEggDrop(self, K, N):
# 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
# class Solution(object):
# def superEggDrop(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
# lo, hi = 1, N
# while lo < hi:
# mi = (lo + hi) // 2
# if f(mi) < N:
# lo = mi + 1
# else:
# hi = mi
# return lo
from scipy.special import comb, factorial, hyp2f1
from math import log2, floor
def h(k,n):
return round(2**n-1-comb(n,k+1)*hyp2f1(1,k-n+1,k+2,-1))
def h2(k,n):
return sum(comb(n,j) for j in range(1,k+1))
def e(k, n):
if k == 1:
return n
return sum(e(k-1, j)+1 for j in range(n))
class Solution:
def superEggDrop(self, k: int, n: int) -> int:
# best_case = floor(log2(n)+1)
# if k >= best_case:
# return best_case
l = 0
while h2(k, l) < n:
l+=1
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 segment_sum(x,k):
# # x is worst case, k is number of eggs available
# # returns max N for worst case x, eggs k
# if k==1:
# return x
# return sum(segment_sum(j, k-1)+1 for j in range(x))
# worst_case=0
# while segment_sum(worst_case, K) < N:
# worst_case+=1
# return worst_case
# class Solution(object):
# def superEggDrop(self, K, N):
# 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
# class Solution(object):
# def superEggDrop(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
# lo, hi = 1, N
# while lo < hi:
# mi = (lo + hi) // 2
# if f(mi) < N:
# lo = mi + 1
# else:
# hi = mi
# return lo
from scipy.special import comb, factorial, hyp2f1
from math import log2, floor
def h(k,n):
return round(2**n-1-comb(n,k+1)*hyp2f1(1,k-n+1,k+2,-1))
def h2(k,n):
return sum(comb(n,j) for j in range(1,k+1))
def e(k, n):
if k == 1:
return n
return sum(e(k-1, j)+1 for j in range(n))
class Solution:
def superEggDrop(self, k: int, n: int) -> int:
best_case = floor(log2(n)+1)
if k >= best_case:
return best_case
l = 0
while h2(k, l) < n:
l+=1
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:
# dp[M][K]means that, given K eggs and M moves, what is the maximum number of floor that we can check.
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
|
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:
'''
# Top down: Time O(KN log(N)) Space O(KN)
import functools
@functools.lru_cache(None)
def dp(k, m):
if k == 1: return m
if m == 1: return 1
return dp(k-1, m-1) + dp(k, m-1) + 1
l, r = 1, N
while l < r:
mid = (l+r) // 2
if dp(K, mid) >= N:
r = mid
else:
l = mid + 1
return l
'''
# Bottom up: Time O(Klog(N)) Space O(K)
dp, ndp = {}, {}
for m in range(1, N+1):
for k in range(1, K+1):
if m == 1: ndp[k] = 1
elif k == 1: ndp[k] = m
else:
ndp[k] = dp[k-1] + dp[k] + 1
if ndp[k] >= N:
return m
dp, ndp = ndp, {}
|
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 segment_sum(x,k):
# # x is worst case, k is number of eggs available
# # returns max N for worst case x, eggs k
# if k==1:
# return x
# return sum(segment_sum(j, k-1)+1 for j in range(x))
# worst_case=0
# while segment_sum(worst_case, K) < N:
# worst_case+=1
# return worst_case
# class Solution(object):
# def superEggDrop(self, K, N):
# 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
# class Solution(object):
# def superEggDrop(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
# lo, hi = 1, N
# while lo < hi:
# mi = (lo + hi) // 2
# if f(mi) < N:
# lo = mi + 1
# else:
# hi = mi
# return lo
from scipy.special import comb, factorial, hyp2f1
from math import log2, floor
def h(k,n):
return round(2**n-1-comb(n,k+1)*hyp2f1(1,k-n+1,k+2,-1))
def h2(k,n):
return sum(comb(n,j) for j in range(1,k+1))
def fast_sum(k,n):
c = n
for i in range(k):
c *= (n-k)/(k+1)
return c
def e(k, n):
if k == 1:
return n
return sum(e(k-1, j)+1 for j in range(n))
class Solution:
def superEggDrop(self, k: int, n: int) -> int:
# best_case = floor(log2(n)+1)
# if k >= best_case:
# return best_case
if k == 0:
return n
l = 0
while h2(k, l) < n:
l+=1
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, eggs: int, floors: int) -> int:
dp = [[0] * (eggs+1) for _ in range(floors+1)]
currFloor = 0
while dp[currFloor][eggs] < floors:
currFloor += 1
for i in range(1 , eggs+1):
dp[currFloor][i] = dp[currFloor - 1][i-1] + dp[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 = [[0 for _ in range(N +1)] for _ in range(K + 1)]
m = 0
while dp[K][m] < N:
m += 1
for k in range(1, K + 1):
dp[k][m] = 1 + dp[k-1][m-1] + dp[k][m-1]
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=[[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
|
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 ncr(self,n, r):
sum=0
r=min(n,r)
for l in range(1,r+1):
sum+=comb(n,l)
return sum
def superEggDrop(self, K, N):
matrix=[[0 for j in range(K)]for i in range(N)]
for i in range(N):
for j in range(K):
matrix[i][j]=self.ncr(i+1,j+1)
if matrix[i][K-1]>=N:
return i+1
print(matrix)
|
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(K+1)] for _ in range(N+1)]
attempt = 0
while dp[attempt][K] < N:
attempt += 1
for i in range(1, K+1):
dp[attempt][i] = 1 + dp[attempt-1][i-1] + dp[attempt-1][i]
if dp[attempt][i] >= N :
return attempt
return attempt
|
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 ncr(self,n, r):
sum=0
r=min(n,r)
for l in range(1,r+1):
sum+=comb(n,l)
return sum
def superEggDrop(self, K, N):
matrix=[[0 for j in range(K)]for i in range(N)]
for i in range(N):
for j in range(K):
matrix[i][j]=self.ncr(i+1,j+1)
if matrix[i][K-1]>=N:
return i+1
print(matrix)
# for m in range(N):
# if matrix[m][K-1]>=N:
# return m+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 = [[0 for _ in range(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:
# dp=[[0 for _ in range(K+1)] for _ in range(N+1)];
# for n in range(N+1):
# dp[n][0]=float('inf');
# for k in range(K+1):
# dp[0][k]=0;
# for k in range(1,K+1):
# for n in range(1,N+1):
# # res= float('inf');
# # for i in range(1,n+1):
# # res= min(res, max(dp[i-1][k-1],dp[n-i][k]));
# # dp[n][k]=1+res;
# l=1;
# r=n;
# while(r-l>=2):
# mid=int((l+r)/2);
# a= dp[mid-1][k-1];
# b= dp[n-mid][k];
# if a>b:
# r=mid-1;
# elif a<b:
# l=mid+1;
# else:
# l,r=mid,mid;
# dp[n][k]= 1 + min( max(dp[l-1][k-1],dp[n-l][k]) , max(dp[r-1][k-1],dp[n-r][k]));
# return dp[N][K];
# O(k*N) time
dp= [[0 for _ in range(K+1)] for _ in range(N+1)];
for n in range(1,N+1):
for k in range(1,K+1):
dp[n][k]=1 + dp[n-1][k] + dp[n-1][k-1];
if dp[n][K]>=N:
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 superEggDrop(self, K: int, N: int) -> int:
def dp(m, k):
if m<=1 or k==1: return m
if (m, k) in memo: return memo[(m, k)]
memo[(m, k)] = dp(m-1, k-1)+1+dp(m-1, k)
return memo[(m, k)]
memo = {}
for i in range(N+1):
if dp(i, K)>=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:
# drops = 0
# floors = [0] * (K + 1)
# while floors[K] < N:
# for eggs in range(K, 0, -1):
# floors[eggs] += 1 + floors[eggs - 1]
# drops += 1
# return drops
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 = N # 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 for j in range(K+1)] for i in range(N+1)]
attempt = 0
while(dp[attempt][K]<N):
attempt+=1
for j in range(1,len(dp[0])):
dp[attempt][j]= 1 + dp[attempt-1][j-1]+ dp[attempt-1][j]
if dp[attempt][j]>=N:
return attempt
|
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:
drops = 0
floors = [0] * (K + 1)
while floors[K] < N:
for eggs in range(K, 0, -1):
floors[eggs] += 1 + floors[eggs - 1]
drops += 1
return drops
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 = N # big enough number
dp = [[0 for j in range(K+1)] for i in range(M+1)]
# --> 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 for i in range(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]=1+dp[m-1][k-1]+dp[m-1][k]
if dp[m][k]>=N:
return m
#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 = [[0 for i in range(K + 1)] for j 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] + 1 + dp[m - 1][k]
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
|
import numpy as np
class Solution:
def superEggDrop(self, K: int, N: int) -> int:
# given K eggs and M moves, what is the maximum number of floor that we can check.
dp = [[0 for i in range(K+1)] for j in range(N+1)]
for i in range(1, N+1):
for j in range(1, K+1):
dp[i][j] = dp[i-1][j-1] + dp[i-1][j] + 1
if dp[i][j] >= N:
return i
# #Dec1, 2019
# self.grid = np.zeros((K+1, N+1), dtype=np.int32)
# return self.drop(K, N)
# def drop(self, K, N):
# for j in range(1, N+1):
# for i in range(1, K+1):
# self.grid[i][j] = self.grid[i-1][j-1] + self.grid[i][j-1] + 1
# if self.grid[K][j] >= N: #if we could already check all stairs with K eggs, then return the number of stairs j
# return j
# return self.grid[K][N]
# #Dec1, 2019
# self.grid = np.zeros((K+1, N+1), dtype=np.int32)
# return self.test(K, N)
# def test(self, K, N):
# if N == 1 or N == 0:
# return N
# elif K == 1 or K == 0:
# return N
# else:
# if self.grid[K][N]:
# return self.grid[K][N]
# else:
# self.grid[K][N] = min(max(self.test(K-1, i-1), self.test(K, N-i)) for i in range(1, N+1))+1
# return self.grid[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
|
#O(N*K)
#O(1)
class Solution:
def superEggDrop(self, K: int, N: int) -> int:
#find max no of floor you can reach with given eggs and no of attempts
dp = [ [0 for j in range(K+1)] for i in range(N+1)]
attempt = 0
while(dp[attempt][K]<N):
attempt+=1
for j in range(1,len(dp[0])):
# 1 + Break case + No Break case
dp[attempt][j]= 1 + dp[attempt-1][j-1]+ dp[attempt-1][j]
if dp[attempt][j]>=N:
return attempt
|
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
from typing import Tuple
class Solution:
# floor: 0 <= F <= N
# Ans f where eggs break if it's higher than f
# N floors from 1 to N.
# K eggs
# Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).
def superEggDrop(self, K: int, N: int) -> int:
return superEggHelper(K, N)
@lru_cache(maxsize=None)
def superEggHelper(K: int, N: int) -> int:
# base case
if N == 0:
return 0
if K == 1:
return N
# ans = N;
# dropping from floor i
# for i in range(1, N + 1):
# ans = min(max(superEggHelper(K, N - i), superEggHelper(K - 1, i - 1)) + 1, ans)
# bin search
ans = bisectEggHelper(K, 1, N, N)
# print(K, N, ans)
return ans
# 2 0 1 1
# S: start, E: end, N: number of floors, K: number of eggs
def bisectEggHelper(K: int, S: int, E: int, N: int) -> int:
# print(K, S, E, N)
# base case, only one element left
if S == E:
left, right = superEggCalc(K, S, N)
return max(left, right) + 1
# base case, only two elements left
if S + 1 == E:
# K = 2
# i = 0
# S = 0
# N = 1
left1, right1 = superEggCalc(K, S, N)
left2, right2 = superEggCalc(K, E, N)
return min(max(left1, right1), max(left2, right2)) + 1
# choose i = (S + E) // 2
i = (S + E) // 2
left, right = superEggCalc(K, i, N)
if left > right:
return bisectEggHelper(K, S, i, N)
else:
return bisectEggHelper(K, i, E, N)
def superEggCalc(K: int, i: int, N: int) -> Tuple:
return (superEggHelper(K - 1, i - 1), superEggHelper(K, N - 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 = list(range(1, N+1))
for _ in range(K-1):
temp = [1]
for j in range(1, N):
if temp[-1] >= N:
break
temp.append(temp[-1]+dp[j-1]+1)
dp = temp
return len(dp)
|
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 segment_sum(x,k):
# # x is worst case, k is number of eggs available
# # returns max N for worst case x, eggs k
# if k==1:
# return x
# return sum(segment_sum(j, k-1)+1 for j in range(x))
# worst_case=0
# while segment_sum(worst_case, K) < N:
# worst_case+=1
# return worst_case
# class Solution(object):
# def superEggDrop(self, K, N):
# 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
# class Solution(object):
# def superEggDrop(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
# lo, hi = 1, N
# while lo < hi:
# mi = (lo + hi) // 2
# if f(mi) < N:
# lo = mi + 1
# else:
# hi = mi
# return lo
from scipy.special import comb, factorial, hyp2f1
from math import log2, floor
def h(k,n):
return round(2**n-1-comb(n,k+1)*hyp2f1(1,k-n+1,k+2,-1))
def e(k, n):
if k == 1:
return n
return sum(e(k-1, j)+1 for j in range(n))
class Solution:
def superEggDrop(self, k: int, n: int) -> int:
best_case = floor(log2(n)+1)
if k >= best_case:
return best_case
l = 0
while e(k, l) < n:
l+=1
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:
@lru_cache(None)
def superEggDrop(self, k: int, n: int) -> int:
if n == 0:
return 0
elif k == 1:
return n
lo, hi = 1, n
while lo < hi:
mid = (lo + hi) // 2
r1, r2 = self.superEggDrop(k-1, mid-1), self.superEggDrop(k, n - mid)
if r1 > r2:
hi = mid
elif r1 < r2:
lo = mid + 1
else:
lo = hi = mid
return 1 + max(self.superEggDrop(k-1, lo-1), self.superEggDrop(k, n-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:
#6:00
# if we have 1 one egg
# we will need N attempts
# if we have 2 egg
# pick start postion as any k
# if it breaks
# then we will need f(k-1,1)
# if it doesn't break f(n-k+1,2)
@lru_cache(None)
def helper(floors,eggs):
if floors<=0:return 0
if eggs==1:return floors
# pick k
ans=floors
low,high=1,floors
while low+1<high:
# we dont low, high to be together
mid=low+(high-low)//2
t1=helper(mid-1,eggs-1)
t2=helper(floors-mid,eggs)
if t1<t2:
low=mid
elif t1>t2:
high=mid
else:
low=high=mid
ans=floors
for k in (low,high):
ans=min(ans,1+max(helper(k-1,eggs-1),helper(floors-k,eggs)))
# for k in range(1,floors+1):
# # can it be binary search? i think graph has to be symmetric
# ans=min(ans,1+max(helper(k-1,eggs-1),helper(floors-k,eggs)))
return ans
return helper(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
|
from functools import lru_cache
class Solution:
def superEggDrop(self, K: int, N: int) -> int:
@lru_cache(maxsize=None)
def dp(K: int, N: int) -> int:
if N==0:
return 0
elif K==1:
return N
else:
l,r=1,N
res=N
while l+1<r:
X=(l+r)//2
f1=dp(K,N-X)
f2=dp(K-1,X-1)
if f1<f2:
r=X
elif f1==f2:
l=r=X
else:
l=X
return 1+min(max(dp(K-1,X-1),dp(K,N-X)) for X in (l,r))
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 sys
class Solution:
def superEggDrop(self, K: int, N: int) -> int:
val = Solution.dp(K,N)
return val
__memo = dict()
def dp(K,N):
if K == 1:
return N
if N == 0:
return 0
if (K,N) in Solution.__memo:
return Solution.__memo[(K,N)]
res = sys.maxsize
lo, hi = 1, N
while lo <= hi:
mid = (lo + hi) // 2
broken = Solution.dp(K - 1, mid - 1) # 碎
not_broken = Solution.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)
Solution.__memo[(K,N)] = res
return res
|
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
|
t = [[-1 for i in range(10001)] for j in range(101)]
class Solution:
def superEggDrop(self, K: int, N: int) -> int:
if N == 0 or N == 1:
return N
if K == 1:
return N
if t[K][N] != -1:
return t[K][N]
mn, l, h = sys.maxsize, 1, N
while l <= h:
mid = (l + h) // 2
down_temp = self.superEggDrop(K - 1, mid - 1)
up_temp = self.superEggDrop(K, N - mid)
res = 1 + max(down_temp, up_temp)
if down_temp < up_temp:
l = mid + 1
else:
h = mid - 1
mn = min(mn, res)
t[K][N] = mn
return mn
|
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:
dict_soln = {}
return self.dp(K, N, dict_soln)
def dp(self, k, n, dict_soln):
if n == 0:
return 0
if k == 0:
return -1
if k == 1:
return n
if (k, n) in dict_soln:
return dict_soln[(k, n)]
# test at x (x = 1, ..., n)
# dp(k, n) = min_{x = 1, ..., n} max(dp(k, n - x), dp(k - 1, x - 1)) + 1
left = 1
right = n
while True:
if left - right == -1:
tmp1 = self.dp(k - 1, left - 1, dict_soln)
tmp2 = self.dp(k, n - left, dict_soln)
tmp3 = self.dp(k - 1, right - 1, dict_soln)
tmp4 = self.dp(k, n - right, dict_soln)
tmpl = max(tmp1, tmp2)
tmpr = max(tmp3, tmp4)
if tmpl < tmpr:
dict_soln[(k, n)] = tmpl + 1
return dict_soln[(k, n)]
else:
dict_soln[(k, n)] = tmpr + 1
return dict_soln[(k, n)]
xmid = (left + right) // 2
tmp1 = self.dp(k - 1, xmid - 1, dict_soln)
tmp2 = self.dp(k, n - xmid, dict_soln)
if tmp1 > tmp2:
right = xmid
elif tmp1 < tmp2:
left = xmid
else:
dict_soln[(k, n)] = tmp1 + 1
return dict_soln[(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):
# 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)
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)
elif not_broken > broken:
lo = mid + 1
res = min(res, not_broken + 1)
elif not_broken == broken:
res = min(res, broken + 1)
break
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:
### state: dp[k][m]. 如果有K个鸡蛋,最多能抛m次,那么最多可以确定的F最高为几层楼
### transfer: 如果在某一层楼丢鸡蛋,要不就是碎了,要不就是没碎。
#### 碎了:说明得测楼下: dp[k-1][m-1]为最多能测落下的层数
#### 没碎:说明得测楼上: dp[k][m-1]为最多能测楼上的层数
####不管碎还是没碎,总共能测的层数= 楼下 + 楼上 + 1(现在那一层)
#### dp[k][m] = dp[k-1][m-1] + dp[k][m-1] + 1
#### 每当有一个m值的时候,我们也要从k=1 到k=K去simulate,去填充dp里面的值(dp来自上面和左上角)
### init: base case是什么呢?比如K=0的时候,肯定测0层。 m=0的时候,也测0层
#### dp[0][...] = 0; dp[...][0] = 0
### result: 当dp[K][m] == N 的时候,m就是我们想要的答案. K是大写K,因为我们一开始就有K个鸡蛋
dp = [[0]*(N+1) for i in range(K+1)]
m = 0
while dp[K][m] < N:
m += 1
for i in range(1, K+1):
dp[i][m] = dp[i][m-1] + dp[i-1][m-1] + 1
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:
memo = {}
def dp(k, n):
if (k, n) in memo: return memo[(k, n)]
if k == 1:
return n
if n == 0:
return 0
start = 1
end = n
res = float('inf')
while start <= end:
mid = (start+end)//2
pre = dp(k-1, mid-1)+1
cur = dp(k, n-mid)+1
if pre < cur:
res = min(res, cur)
start = mid+1
elif pre > cur:
res = min(res, pre)
end = mid-1
else:
res = pre
break
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 = list(range(1, N+1))
for _ in range(K-1):
temp = [1]
for j in range(1, N):
temp.append(temp[-1]+dp[j-1]+1)
dp = temp
for i in range(N):
if dp[i] >= N:
return i+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 = list(range(1, N+1))
for _ in range(K-1):
temp = [1]
for j in range(1, N):
temp.append(temp[-1]+dp[j-1]+1)
dp = temp
return [i+1 for i, x in enumerate(dp) if x >= N][0]
|
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)
return self.bottomUp2(K, N)
@lru_cache(maxsize = None)
def findMoves(self, K, N):
if K == 1 or N<=1:
return 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))
# f = N
# 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):
# compress to 1d array
dp = [i for i in range(N+1)]
for i in range(2, K+1):
prev = dp[:]
for j in range(1, N+1):
min_drops = N
for x in range(1, j+1):
min_drops = min(min_drops, 1 + max(prev[x-1], dp[j-x]))
dp[j] = min_drops
print(dp)
return dp[N]
def bottomUp2(self, K, N):
dp = [i for i in range(N+1)]
for i in range(2, K+1):
broken = dp[1]
for i in range(2, N+1):
prev = dp[i]
dp[i] = 1+dp[i-1]+broken
broken = prev
left, right = 1, N
while left < right:
mid = (left + right) // 2
if dp[mid] >= N:
right = mid
left = left + 1
return left
|
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 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
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:
# def solve(e,f,dp):
# if f==0:
# return f
# elif e==1:
# return f
#
# if dp[e][f]!=-1:
# return dp[e][f]
# l,h=1,f
# while l+1<h:
# x=(l+h)//2
# if dp[e][f-x]!=-1:
# f1=dp[e][f-x]
# else:
# f1=solve(e,f-x,dp)
# if dp[e-1][x-1]!=-1:
# f2=dp[e-1][x-1]
# else:
# f2=solve(e-1,x-1,dp)
# if f1<f2:
# l=x
# elif f2>f1:
# h=x
# else:
# l=h=x
#
# ans=1+min(max(solve(e,f-x,dp),solve(e,x-1,dp)) for x in range(l,h+1))
# dp[e][f]=ans
# return dp[e][f]
# dp=[[-1]*(N+1) for i in range(K+1)]
# return solve(K,N,dp)
class Solution(object):
def superEggDrop(self, K, N):
memo = {}
def dp(k, n):
if (k, n) not in memo:
if n == 0 or n==1:
ans = n
elif k == 1:
ans = n
else:
lo, hi = 1, n
# keep a gap of 2 X values to manually check later
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=1000000
for x in range(lo, hi+1):
temp=1+max(dp(k-1, x-1), dp(k, n-x))
if ans>temp:
ans=temp
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 solve(e,f,dp):
# if f==0:
# return f
# elif e==1:
# return f
#
# if dp[e][f]!=-1:
# return dp[e][f]
# l,h=1,f
# while l+1<h:
# x=(l+h)//2
# if dp[e][f-x]!=-1:
# f1=dp[e][f-x]
# else:
# f1=solve(e,f-x,dp)
# if dp[e-1][x-1]!=-1:
# f2=dp[e-1][x-1]
# else:
# f2=solve(e-1,x-1,dp)
# if f1<f2:
# l=x
# elif f2>f1:
# h=x
# else:
# l=h=x
#
# ans=1+min(max(solve(e,f-x,dp),solve(e,x-1,dp)) for x in range(l,h+1))
# dp[e][f]=ans
# return dp[e][f]
# dp=[[-1]*(N+1) for i in range(K+1)]
# return solve(K,N,dp)
class Solution(object):
def superEggDrop(self, K, N):
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 + 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=1000000
for x in range(lo, hi+1):
temp=1+max(dp(k-1, x-1), dp(k, n-x))
if ans>temp:
ans=temp
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, N):
dp = dict()
def recursive(k, n):
if k == 1:
return n
if n == 0:
return 0
if (k, n) in dp:
return dp[(k, n)]
res = float('INF')
l = 1
r = n + 1
while l <= r:
m = int((l + r) / 2)
v1 = recursive(k, n - m)
v2 = recursive(k - 1, m - 1)
res = min(res, max(v1, v2) + 1)
if v1 < v2:
r = m - 1
elif v1 > v2:
l = m + 1
else:
break
dp[(k, n)] = res
return res
import sys
sys.setrecursionlimit(3000)
res = recursive(K, N)
return res
|
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 helper(K, N):
if N <= 2:
return N
if K == 1:
return N
if (K, N) not in dp:
l = 1
r = N
while l + 1 < r:
mid = (l + r) // 2
# if egg breaks at midpoint, try egg drop for lower floors
die = helper(K - 1, mid - 1)
# if egg survives, try egg drop for higher floors
live = helper(K, N - mid)
if die < live:
l = mid
elif die > live:
r = mid
else:
l = r = mid
dp[(K, N)] = 1 + min([max(helper(K - 1, l - 1), helper(K, N - l)), max(helper(K - 1, r - 1), helper(K, N - r))])
return dp[(K, N)]
return helper(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 k in range(1, K + 1):
next_max_floors = [0 for _ in range(N + 1)]
for t in range(1, N + 1):
next_max_floors[t] = 1 + max_floors[t - 1] + next_max_floors[t - 1]
max_floors = next_max_floors
for t in range(N):
if max_floors[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 __init__(self):
self.memo = {}
def superEggDrop(self, K: int, N: int) -> int:
def solve(K, N):
d = self.memo.get((K, N))
if d is None:
if K == 1 or N <= 2:
# 1) K == 1: Only one egg is left.
# Drop egg from bottom floor up until it breaks.
# Maximum N drops.
# 2) N <= 2: Simple cases that don't need binary search.
# Same strategy as above.
d = N
else:
A_min = 1
A_max = N
while A_max - A_min > 1:
A = (A_min + A_max) // 2
t1 = solve(K - 1, A - 1)
t2 = solve(K, N - A)
if t1 > t2:
A_max = A
elif t2 > t1:
A_min = A
else:
A_min = A_max = A
d = 1 + min(
max(
self.memo[K - 1, A - 1],
self.memo[K, N - A]
)
for A in [A_min, A_max]
)
self.memo[K, N] = d
return d
return solve(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 = {}
def helper(K, N):
if N <= 1:
dp[(K, N)] = N
if K == 1:
dp[(K, N)] = N
if (K, N) not in dp:
l = 1
r = N
while l + 1 < r:
mid = (l + r) // 2
# if egg breaks at midpoint, try egg drop for lower floors
die = helper(K - 1, mid - 1)
# if egg survives, try egg drop for higher floors
live = helper(K, N - mid)
if die < live:
l = mid
elif die > live:
r = mid
else:
l = r = mid
dp[(K, N)] = 1 + min([max(helper(K - 1, l - 1), helper(K, N - l)), max(helper(K - 1, r - 1), helper(K, N - r))])
return dp[(K, N)]
return helper(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 segment_sum(x,k):
# # x is worst case, k is number of eggs available
# # returns max N for worst case x, eggs k
# if k==1:
# return x
# return sum(segment_sum(j, k-1)+1 for j in range(x))
# worst_case=0
# while segment_sum(worst_case, K) < N:
# worst_case+=1
# return worst_case
# class Solution(object):
# def superEggDrop(self, K, N):
# 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
# class Solution(object):
# def superEggDrop(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
# lo, hi = 1, N
# while lo < hi:
# mi = (lo + hi) // 2
# if f(mi) < N:
# lo = mi + 1
# else:
# hi = mi
# return lo
# from scipy.special import comb, factorial, hyp2f1
# from math import log2, floor
# def h(k,n):
# return round(2**n-1-comb(n,k+1)*hyp2f1(1,k-n+1,k+2,-1))
# def h2(k,n):
# return sum(comb(n,j) for j in range(1,k+1))
# def fast_sum(k,l):
# s = 0
# c = 1
# for i in range(1,k+1):
# c *= (l-i+1)/(i)
# s += c
# return s
# def e(k, n):
# if k == 1:
# return n
# return sum(e(k-1, j)+1 for j in range(n))
from math import floor, log2
class Solution:
def superEggDrop(self, k: int, n: int) -> int:
best_case = floor(log2(n)+1)
if k >= best_case:
return best_case
def fast_sum(k,l):
s = 0
c = 1
for i in range(k):
c *= (l-i)/(i+1)
s += c
return s
if k == 0:
return n
a, b = 1, n
while a < b:
c = (a+b)//2
if fast_sum(k,c) < n:
a = c + 1
else:
b = c
return a
|
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(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:
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 - lo) // 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:
INF = N + 1
cache = {}
def eggDropHelper(k, n):
if (k, n) in cache:
return cache[(k, n)]
if (k < 0):
return INF
if (n == 0):
return 0
if (k == 0):
return INF
optAns = INF
l, r = 1, n
while ((l + 1) < r):
m = (l + r) // 2
left, right = eggDropHelper(k - 1, m - 1), eggDropHelper(k, n - m)
if (left < right):
l = m
elif (left > right):
r = m
else:
l = r = m
break
optAns = max(eggDropHelper(k - 1, l - 1), eggDropHelper(k, n - l)) + 1
currAns = max(eggDropHelper(k - 1, r - 1), eggDropHelper(k, n - r)) + 1
optAns = min(currAns, optAns)
cache[(k, n)] = optAns
return cache[(k, n)]
return eggDropHelper(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):
ans = 0
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:
r = x
elif t1 < t2:
l = 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
|
class Solution:
def superEggDrop(self, K, N):
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 + 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(object):
def superEggDrop(self, K, N):
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 + 1 < hi:
x = (lo + hi) >> 1
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 = t1
# if lo == hi:
# ans = t1
# else:
# ans = min(t1,t2)
# print(lo,hi)
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]
else:
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, N):
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 + 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)
|
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s.
Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.
Example 1:
Input: s = "annabelle", k = 2
Output: true
Explanation: You can construct two palindromes using all characters in s.
Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
Example 2:
Input: s = "leetcode", k = 3
Output: false
Explanation: It is impossible to construct 3 palindromes using all the characters of s.
Example 3:
Input: s = "true", k = 4
Output: true
Explanation: The only possible solution is to put each character in a separate string.
Example 4:
Input: s = "yzyzyzyzyzyzyzy", k = 2
Output: true
Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome.
Example 5:
Input: s = "cr", k = 7
Output: false
Explanation: We don't have enough characters in s to construct 7 palindromes.
Constraints:
1 <= s.length <= 10^5
All characters in s are lower-case English letters.
1 <= k <= 10^5
|
from collections import Counter
class Solution:
def canConstruct(self, s: str, k: int) -> bool:
if k > len(s): #return False
return False
counter = Counter(s)
odd_counts = 0
for char in counter:
if counter[char] % 2 == 1:
odd_counts += 1
return odd_counts <= k
|
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s.
Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.
Example 1:
Input: s = "annabelle", k = 2
Output: true
Explanation: You can construct two palindromes using all characters in s.
Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
Example 2:
Input: s = "leetcode", k = 3
Output: false
Explanation: It is impossible to construct 3 palindromes using all the characters of s.
Example 3:
Input: s = "true", k = 4
Output: true
Explanation: The only possible solution is to put each character in a separate string.
Example 4:
Input: s = "yzyzyzyzyzyzyzy", k = 2
Output: true
Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome.
Example 5:
Input: s = "cr", k = 7
Output: false
Explanation: We don't have enough characters in s to construct 7 palindromes.
Constraints:
1 <= s.length <= 10^5
All characters in s are lower-case English letters.
1 <= k <= 10^5
|
class Solution:
def canConstruct(self, s: str, k: int) -> bool:
from collections import Counter
if len(s) < k:
return False
oc = 0
for _ , val in list(Counter(s).items()):
if val % 2 == 1:
oc += 1
return False if oc > k else True
# return False if k < len([1 for _ , val in Counter(s).items() if val % 2 == 1]) else True
|
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s.
Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.
Example 1:
Input: s = "annabelle", k = 2
Output: true
Explanation: You can construct two palindromes using all characters in s.
Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
Example 2:
Input: s = "leetcode", k = 3
Output: false
Explanation: It is impossible to construct 3 palindromes using all the characters of s.
Example 3:
Input: s = "true", k = 4
Output: true
Explanation: The only possible solution is to put each character in a separate string.
Example 4:
Input: s = "yzyzyzyzyzyzyzy", k = 2
Output: true
Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome.
Example 5:
Input: s = "cr", k = 7
Output: false
Explanation: We don't have enough characters in s to construct 7 palindromes.
Constraints:
1 <= s.length <= 10^5
All characters in s are lower-case English letters.
1 <= k <= 10^5
|
class Solution:
def canConstruct(self, s: str, k: int) -> bool:
n = len(s)
if n < k:
return False
if n == k:
return True
ch_to_cnt = collections.defaultdict(int)
for ch in s:
ch_to_cnt[ch] += 1
for cnt in ch_to_cnt.values():
if cnt %2 == 1:
k -= 1
return k >= 0
|
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s.
Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.
Example 1:
Input: s = "annabelle", k = 2
Output: true
Explanation: You can construct two palindromes using all characters in s.
Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
Example 2:
Input: s = "leetcode", k = 3
Output: false
Explanation: It is impossible to construct 3 palindromes using all the characters of s.
Example 3:
Input: s = "true", k = 4
Output: true
Explanation: The only possible solution is to put each character in a separate string.
Example 4:
Input: s = "yzyzyzyzyzyzyzy", k = 2
Output: true
Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome.
Example 5:
Input: s = "cr", k = 7
Output: false
Explanation: We don't have enough characters in s to construct 7 palindromes.
Constraints:
1 <= s.length <= 10^5
All characters in s are lower-case English letters.
1 <= k <= 10^5
|
from collections import defaultdict
class Solution:
def canConstruct(self, s: str, k: int) -> bool:
d = defaultdict(int)
if len(s) < k:
return False
for char in s:
d[char] += 1
odd = 0
for key in d:
print(key)
if d[key] % 2 != 0:
odd += 1
return odd <= k
|
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s.
Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.
Example 1:
Input: s = "annabelle", k = 2
Output: true
Explanation: You can construct two palindromes using all characters in s.
Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
Example 2:
Input: s = "leetcode", k = 3
Output: false
Explanation: It is impossible to construct 3 palindromes using all the characters of s.
Example 3:
Input: s = "true", k = 4
Output: true
Explanation: The only possible solution is to put each character in a separate string.
Example 4:
Input: s = "yzyzyzyzyzyzyzy", k = 2
Output: true
Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome.
Example 5:
Input: s = "cr", k = 7
Output: false
Explanation: We don't have enough characters in s to construct 7 palindromes.
Constraints:
1 <= s.length <= 10^5
All characters in s are lower-case English letters.
1 <= k <= 10^5
|
from collections import defaultdict
class Solution:
def canConstruct(self, s: str, k: int) -> bool:
d = defaultdict(int)
if len(s) < k:
return False
for char in s:
d[char] += 1
odd = 0
for key in d:
if d[key] % 2 != 0:
odd += 1
return odd <= k
|
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s.
Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.
Example 1:
Input: s = "annabelle", k = 2
Output: true
Explanation: You can construct two palindromes using all characters in s.
Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
Example 2:
Input: s = "leetcode", k = 3
Output: false
Explanation: It is impossible to construct 3 palindromes using all the characters of s.
Example 3:
Input: s = "true", k = 4
Output: true
Explanation: The only possible solution is to put each character in a separate string.
Example 4:
Input: s = "yzyzyzyzyzyzyzy", k = 2
Output: true
Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome.
Example 5:
Input: s = "cr", k = 7
Output: false
Explanation: We don't have enough characters in s to construct 7 palindromes.
Constraints:
1 <= s.length <= 10^5
All characters in s are lower-case English letters.
1 <= k <= 10^5
|
class Solution:
def canConstruct(self, s: str, k: int) -> bool:
c = Counter(s)
return sum(c.values()) >= k and len([v for v in c.values() if v % 2]) <= k
|
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s.
Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.
Example 1:
Input: s = "annabelle", k = 2
Output: true
Explanation: You can construct two palindromes using all characters in s.
Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
Example 2:
Input: s = "leetcode", k = 3
Output: false
Explanation: It is impossible to construct 3 palindromes using all the characters of s.
Example 3:
Input: s = "true", k = 4
Output: true
Explanation: The only possible solution is to put each character in a separate string.
Example 4:
Input: s = "yzyzyzyzyzyzyzy", k = 2
Output: true
Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome.
Example 5:
Input: s = "cr", k = 7
Output: false
Explanation: We don't have enough characters in s to construct 7 palindromes.
Constraints:
1 <= s.length <= 10^5
All characters in s are lower-case English letters.
1 <= k <= 10^5
|
class Solution:
def canConstruct(self, s: str, k: int) -> bool:
if len(s) < k:
return False
numodds = 0
keys = collections.defaultdict(int)
for c in s:
keys[c] += 1
for key in keys:
if keys[key] % 2:
numodds += 1
return numodds <= k
|
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s.
Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.
Example 1:
Input: s = "annabelle", k = 2
Output: true
Explanation: You can construct two palindromes using all characters in s.
Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
Example 2:
Input: s = "leetcode", k = 3
Output: false
Explanation: It is impossible to construct 3 palindromes using all the characters of s.
Example 3:
Input: s = "true", k = 4
Output: true
Explanation: The only possible solution is to put each character in a separate string.
Example 4:
Input: s = "yzyzyzyzyzyzyzy", k = 2
Output: true
Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome.
Example 5:
Input: s = "cr", k = 7
Output: false
Explanation: We don't have enough characters in s to construct 7 palindromes.
Constraints:
1 <= s.length <= 10^5
All characters in s are lower-case English letters.
1 <= k <= 10^5
|
class Solution:
def canConstruct(self, s: str, k: int) -> bool:
if k > len(s):
return False
dictionary = {}
for character in s:
if character in dictionary:
dictionary[character] += 1
else:
dictionary[character] = 1
odd_occurences = 0
for e in list(dictionary.values()):
odd_occurences += e % 2
if odd_occurences > k:
return False
return True
|
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s.
Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.
Example 1:
Input: s = "annabelle", k = 2
Output: true
Explanation: You can construct two palindromes using all characters in s.
Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
Example 2:
Input: s = "leetcode", k = 3
Output: false
Explanation: It is impossible to construct 3 palindromes using all the characters of s.
Example 3:
Input: s = "true", k = 4
Output: true
Explanation: The only possible solution is to put each character in a separate string.
Example 4:
Input: s = "yzyzyzyzyzyzyzy", k = 2
Output: true
Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome.
Example 5:
Input: s = "cr", k = 7
Output: false
Explanation: We don't have enough characters in s to construct 7 palindromes.
Constraints:
1 <= s.length <= 10^5
All characters in s are lower-case English letters.
1 <= k <= 10^5
|
class Solution:
def canConstruct(self, s: str, k: int) -> bool:
if k > len(s):
return False
dictionary = {}
for character in s:
if character in dictionary:
dictionary[character] += 1
else:
dictionary[character] = 1
odd_occurences = 0
for e in dictionary.values():
odd_occurences += e % 2
if odd_occurences > k:
return False
return True
|
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s.
Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.
Example 1:
Input: s = "annabelle", k = 2
Output: true
Explanation: You can construct two palindromes using all characters in s.
Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
Example 2:
Input: s = "leetcode", k = 3
Output: false
Explanation: It is impossible to construct 3 palindromes using all the characters of s.
Example 3:
Input: s = "true", k = 4
Output: true
Explanation: The only possible solution is to put each character in a separate string.
Example 4:
Input: s = "yzyzyzyzyzyzyzy", k = 2
Output: true
Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome.
Example 5:
Input: s = "cr", k = 7
Output: false
Explanation: We don't have enough characters in s to construct 7 palindromes.
Constraints:
1 <= s.length <= 10^5
All characters in s are lower-case English letters.
1 <= k <= 10^5
|
class Solution:
def canConstruct(self, s: str, k: int) -> bool:
if k > len(s):
return False
dictionary = {}
for character in s:
if character in dictionary:
dictionary[character] += 1
else:
dictionary[character] = 1
print((list(dictionary.values())))
odd_occurences = 0
for e in list(dictionary.values()):
if e % 2 == 1:
odd_occurences += 1
print(odd_occurences)
if odd_occurences > k:
return False
return True
|
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s.
Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.
Example 1:
Input: s = "annabelle", k = 2
Output: true
Explanation: You can construct two palindromes using all characters in s.
Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
Example 2:
Input: s = "leetcode", k = 3
Output: false
Explanation: It is impossible to construct 3 palindromes using all the characters of s.
Example 3:
Input: s = "true", k = 4
Output: true
Explanation: The only possible solution is to put each character in a separate string.
Example 4:
Input: s = "yzyzyzyzyzyzyzy", k = 2
Output: true
Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome.
Example 5:
Input: s = "cr", k = 7
Output: false
Explanation: We don't have enough characters in s to construct 7 palindromes.
Constraints:
1 <= s.length <= 10^5
All characters in s are lower-case English letters.
1 <= k <= 10^5
|
class Solution:
def is_palindrome(self, s):
return bool(s == s[::-1])
def canConstruct(self, s: str, k: int) -> bool:
r_map = dict()
if k > len(s):
return False
for x in s:
if x not in r_map:
r_map[x] = 1
else:
r_map[x] += 1
odd_count = 0
for key in r_map:
if r_map[key] % 2 == 1:
odd_count += 1
return odd_count <= k
|
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s.
Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.
Example 1:
Input: s = "annabelle", k = 2
Output: true
Explanation: You can construct two palindromes using all characters in s.
Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
Example 2:
Input: s = "leetcode", k = 3
Output: false
Explanation: It is impossible to construct 3 palindromes using all the characters of s.
Example 3:
Input: s = "true", k = 4
Output: true
Explanation: The only possible solution is to put each character in a separate string.
Example 4:
Input: s = "yzyzyzyzyzyzyzy", k = 2
Output: true
Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome.
Example 5:
Input: s = "cr", k = 7
Output: false
Explanation: We don't have enough characters in s to construct 7 palindromes.
Constraints:
1 <= s.length <= 10^5
All characters in s are lower-case English letters.
1 <= k <= 10^5
|
class Solution:
def canConstruct(self, s: str, k: int) -> bool:
dict = {}
for c in s:
if c in dict: dict[c] += 1
else: dict[c] = 1
num_of_distinct = 0
for key in dict.keys():
if dict[key]%2 == 1: num_of_distinct += 1
return num_of_distinct <= k and k <= len(s)
|
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s.
Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.
Example 1:
Input: s = "annabelle", k = 2
Output: true
Explanation: You can construct two palindromes using all characters in s.
Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
Example 2:
Input: s = "leetcode", k = 3
Output: false
Explanation: It is impossible to construct 3 palindromes using all the characters of s.
Example 3:
Input: s = "true", k = 4
Output: true
Explanation: The only possible solution is to put each character in a separate string.
Example 4:
Input: s = "yzyzyzyzyzyzyzy", k = 2
Output: true
Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome.
Example 5:
Input: s = "cr", k = 7
Output: false
Explanation: We don't have enough characters in s to construct 7 palindromes.
Constraints:
1 <= s.length <= 10^5
All characters in s are lower-case English letters.
1 <= k <= 10^5
|
class Solution:
def canConstruct(self, s: str, k: int) -> bool:
if k > len(s):
return False
if k == len(s):
return True
charCount = Counter(s)
countOdd = 0
for count in list(charCount.values()):
if count % 2 != 0:
countOdd += 1
return countOdd <= k
|
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s.
Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.
Example 1:
Input: s = "annabelle", k = 2
Output: true
Explanation: You can construct two palindromes using all characters in s.
Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
Example 2:
Input: s = "leetcode", k = 3
Output: false
Explanation: It is impossible to construct 3 palindromes using all the characters of s.
Example 3:
Input: s = "true", k = 4
Output: true
Explanation: The only possible solution is to put each character in a separate string.
Example 4:
Input: s = "yzyzyzyzyzyzyzy", k = 2
Output: true
Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome.
Example 5:
Input: s = "cr", k = 7
Output: false
Explanation: We don't have enough characters in s to construct 7 palindromes.
Constraints:
1 <= s.length <= 10^5
All characters in s are lower-case English letters.
1 <= k <= 10^5
|
class Solution:
def canConstruct(self, s: str, k: int) -> bool:
r_map = dict()
if k > len(s):
return False
for x in s:
if x not in r_map:
r_map[x] = 1
else:
r_map[x] += 1
odd_count = 0
for key in r_map:
if r_map[key] % 2 == 1:
odd_count += 1
return odd_count <= k
|
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s.
Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.
Example 1:
Input: s = "annabelle", k = 2
Output: true
Explanation: You can construct two palindromes using all characters in s.
Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
Example 2:
Input: s = "leetcode", k = 3
Output: false
Explanation: It is impossible to construct 3 palindromes using all the characters of s.
Example 3:
Input: s = "true", k = 4
Output: true
Explanation: The only possible solution is to put each character in a separate string.
Example 4:
Input: s = "yzyzyzyzyzyzyzy", k = 2
Output: true
Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome.
Example 5:
Input: s = "cr", k = 7
Output: false
Explanation: We don't have enough characters in s to construct 7 palindromes.
Constraints:
1 <= s.length <= 10^5
All characters in s are lower-case English letters.
1 <= k <= 10^5
|
class Solution:
def canConstruct(self, s: str, k: int) -> bool:
if len(s) < k:
return False
d = {}
for c in list(s):
if c in d:
d[c] += 1
else:
d[c] = 1
print(d)
if len(list(d.keys())) == k:
return True
else:
odds = 0
for v in list(d.values()):
if v % 2 == 1:
odds += 1
return False if odds > k else True
|
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s.
Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.
Example 1:
Input: s = "annabelle", k = 2
Output: true
Explanation: You can construct two palindromes using all characters in s.
Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
Example 2:
Input: s = "leetcode", k = 3
Output: false
Explanation: It is impossible to construct 3 palindromes using all the characters of s.
Example 3:
Input: s = "true", k = 4
Output: true
Explanation: The only possible solution is to put each character in a separate string.
Example 4:
Input: s = "yzyzyzyzyzyzyzy", k = 2
Output: true
Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome.
Example 5:
Input: s = "cr", k = 7
Output: false
Explanation: We don't have enough characters in s to construct 7 palindromes.
Constraints:
1 <= s.length <= 10^5
All characters in s are lower-case English letters.
1 <= k <= 10^5
|
class Solution:
def canConstruct(self, s: str, k: int) -> bool:
# return sum(i & 1 for i in collections.Counter(s).values()) <= k <= len(s)
# if len(s) < k: return False
# seen = {}
# odd = 0
# for char in s:
# seen[char] = seen.get(char, 0) + 1
# for val in seen.values():
# if val % 2 == 1:
# odd += 1
# if odd > k: return False
# return True
if len(s) < k:
return False
seen = {}
odd = 0
for letter in s:
seen[letter] = seen.get(letter, 0) + 1
for value in seen.values():
if value % 2 != 0:
odd += 1
if odd > k:
return False
return True
|
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s.
Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.
Example 1:
Input: s = "annabelle", k = 2
Output: true
Explanation: You can construct two palindromes using all characters in s.
Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
Example 2:
Input: s = "leetcode", k = 3
Output: false
Explanation: It is impossible to construct 3 palindromes using all the characters of s.
Example 3:
Input: s = "true", k = 4
Output: true
Explanation: The only possible solution is to put each character in a separate string.
Example 4:
Input: s = "yzyzyzyzyzyzyzy", k = 2
Output: true
Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome.
Example 5:
Input: s = "cr", k = 7
Output: false
Explanation: We don't have enough characters in s to construct 7 palindromes.
Constraints:
1 <= s.length <= 10^5
All characters in s are lower-case English letters.
1 <= k <= 10^5
|
class Solution:
def canConstruct(self, s: str, k: int) -> bool:
count = {}
for char in s:
count[char] = count.get(char,0)+1
# print(len([char for char in count if count[char]%2==1]), count)
return k>=len([char for char in count if count[char]%2==1]) and k<=len(s)
|
Given a string s and an integer k. You should construct k non-empty palindrome strings using all the characters in s.
Return True if you can use all the characters in s to construct k palindrome strings or False otherwise.
Example 1:
Input: s = "annabelle", k = 2
Output: true
Explanation: You can construct two palindromes using all characters in s.
Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"
Example 2:
Input: s = "leetcode", k = 3
Output: false
Explanation: It is impossible to construct 3 palindromes using all the characters of s.
Example 3:
Input: s = "true", k = 4
Output: true
Explanation: The only possible solution is to put each character in a separate string.
Example 4:
Input: s = "yzyzyzyzyzyzyzy", k = 2
Output: true
Explanation: Simply you can put all z's in one string and all y's in the other string. Both strings will be palindrome.
Example 5:
Input: s = "cr", k = 7
Output: false
Explanation: We don't have enough characters in s to construct 7 palindromes.
Constraints:
1 <= s.length <= 10^5
All characters in s are lower-case English letters.
1 <= k <= 10^5
|
class Solution:
def canConstruct(self, s: str, k: int) -> bool:
if len(s)<k:
return False
dic={}
for sym in s:
if sym in dic.keys():
dic[sym]=1-dic[sym]
else:
dic[sym]=1
#num=sum(dic.values())
num=0
for i in dic.values():
num+=i
if num<=k:
return True
else:
return False
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.