2
def getMoneyAmount(self, n):3
# For an interval [l,r], we choose a num, which if incorrect still4
# allows us to know whether the secret# is in either [l,num-1] or5
# [num+1,r]. So, the worst-case (w-c) cost is7
# num + max(w-c cost in [l,num-1], w-c cost in [num+1,r])9
# We do this by recursion and binary search, starting with [1,n].11
@lru_cache(None) # <-- we cache function results to avoid recomputing them12
def dp(l=1, r=n) -> int:14
return 0 # <-- base case for the recursion; one number in [l,r]15
ans = 1000 # <-- the answer for n = 200 is 95217
for choice in range((l + r) // 2, r):18
ans = min(ans, choice + max(dp(l, choice - 1), dp(choice + 1, r)))