The Guess Number Higher or Lower II interview question is a "Medium" variation of the previous game, but with a twist. This time, every time you guess a number x and you are wrong, you must pay $x. You need to find out how much money you need to guarantee a win, regardless of what the secret number is. This means you must find the minimum cost of the worst-case scenario.
Google asks this Game Theory and Dynamic Programming problem to test a candidate's ability to model Min-Max algorithms. It evaluates your understanding of "worst-case" scenarios. You want to minimize the cost, but for any chosen strategy, nature will choose the secret number that maximizes your cost. It requires a 2D DP table to represent sub-ranges.
This problem uses 2D Dynamic Programming (Min-Max).
dp[i][j] as the minimum cost to guarantee a win in the range [i, j].i >= j, dp[i][j] = 0 (no guesses needed, you know the number).[i, j], if you guess x (where ), the worst-case cost is x + max(dp[i][x-1], dp[x+1][j]).x that minimizes this worst-case cost: dp[i][j] = min_over_x(x + max(dp[i][x-1], dp[x+1][j])).Range [1, 3]:
"Minimize the maximum loss" or "Guarantee a win" are classic signs of Min-Max DP. Practice setting up the loops: typically, you iterate over the length of the range first, then the start index, to ensure subproblems are solved before larger ones.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Stone Game IV | Hard | Solve | |
| Stone Game VII | Medium | Solve | |
| Stone Game | Medium | Solve | |
| Stone Game V | Hard | Solve | |
| Stone Game III | Hard | Solve |