Guess Number Higher or Lower II
You are playing a guessing game with the following rules:
- I pick a number between
1andn. - You guess a number. If you guess correctly, you win and pay $0.
- If you guess wrong, you pay the dollar amount equal to your guess, and I tell you whether the actual number is higher or lower.
- You continue guessing until you get the right number.
Given n, return the minimum amount of money you need to guarantee a win.
Examples
Input: 10
Output: 16
Input: 1
Output: 0
Hints
Use minimax DP. Define `dp[i][j]` as the minimum amount needed to guarantee a win for the range `[i, j]`.
For each possible first guess `k` in `[i,j]`, the cost is `k + max(dp[i][k-1], dp[k+1][j])`. Take the minimum over all k.
Base case: `dp[i][i] = 0` (only one number, guaranteed correct). Compute intervals in increasing length order.
Related Problems
Guess Number Higher or Lower II
You are playing a guessing game with the following rules: