ProgrammingVery hardCodeforces-style · placement coding round
What is printed?
What is printed?
from functools import lru_cache
@lru_cache(maxsize=None)
def f(e, n):
if n == 0:
return 0
if e == 1:
return n
lo, hi = 1, n
while lo < hi:
mid = (lo + hi) // 2
if f(e - 1, mid - 1) < f(e, n - mid):
lo = mid + 1
else:
hi = mid
return 1 + max(f(e - 1, lo - 1), f(e, n - lo))
print(f(2, 10), f(1, 7))- A. 4 8
- B. 5 7
- C. 4 7
- D. 3 7
Correct answer: C. 4 7
With one egg every floor must be tested, so f(1, 7) is 7. With two eggs the optimal first drop splits the floors so that the worst case is minimised, which gives 4 trials for 10 floors.
More Python Programming questions
- What is printed? python def lal(a): dp = [dict() for in a] best = 0 for i in range(len(a)): for j in range(i): d = a[i] - a[j] dp[i][d] =…
- What is printed? python def pi(s): p = [0] len(s) k = 0 for i in range(1, len(s)): while k and s[i] != s[k]: k = p[k - 1] if s[i] == s[k]…
- What is printed? python from collections import deque def bip(g): col = {} for s in g: if s in col: continue col[s] = 0 q = deque([s])…
- What is printed? python def mst(n, edges): p = list(range(n)) def f(x): while p[x] != x: x = p[x] return x total = 0 for w, u, v in…
- What is printed? python def candy(r): n = len(r) c = [1] n for i in range(1, n): if r[i] > r[i - 1]: c[i] = c[i - 1] + 1 for i in range(n…
- What is printed? python def mat(a, b, m): return [[sum(a[i][k] b[k][j] for k in range(2)) % m for j in range(2)] for i in range(2)] def…