820. Guessing Game Budget
At a fairground booth the host secretly picks a whole number between 1 and n. You name guesses one at a time, and the host answers "higher", "lower" or "correct". The booth charges an entry fee for every guess you make, and the fee is simply the number you named - the winning guess is charged as well.
You must be able to guarantee finding the number whatever the host picked, so you plan for the worst case: the host chooses the number that makes your total spending as large as possible. Return the smallest amount of money that is enough to guarantee a win when you play with the best possible strategy.
Aim for time O(n^3) and space O(n^2).
Example 1
- Input:
- n = 7
- Output:
- 15
- Explanation:
For example, opening with 5 (fee 5) leaves {1..4} or {6,7}; planning both sides well, the worst-case spend over all possible secrets is 15 and no strategy does better.
Example 2
- Input:
- n = 12
- Output:
- 32
- Explanation:
With twelve candidates the best strategy still has a worst-case spend of 32, because the host always steers you to the dearer side.
Constraints
1 ≤ n ≤ 200
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(n^3)
- Space
- O(n^2)