Every two-player zero-sum game boils down to one question: "If I make this move, what's the best my opponent can do in response?"
"At each number, I either pick it (score += num) or skip it — my opponent plays optimally after me. So I maximize(current + min(opponent's options))."
This is the core insight that connects all 6 problems in this article. Whether you're picking stones from ends, guessing a number, or choosing integers from a set — the framework is identical:
- Define the state — what does the current player need to know? (subarray i..j, range [i,j], bitmask of used numbers)
- List the choices — what actions can the current player take? (pick left/right, pick pivot k, pick number n)
- Evaluate opponent's best response — after each choice, what's the opponent's optimal result from the new state?
- Pick the maximum — I choose the action that maximizes my final outcome, assuming my opponent plays optimally.
The key formula that appears in every minimax DP:
// General minimax recurrence
dp[state] = max over each choice (
myImmediateGain + (totalRemaining - dp[opponentState])
)
// Simplified when game is "I take, you take from remainder":
dp[state] = max over each choice (
myImmediateGain - dp[opponentState]
)
// Because total = myGain + opponentGain, so opponentGain = total - myGain
// And if dp tracks "net advantage": myGain - opponentGain = 2*myGain - totalLet's see how this plays out across 6 classic problems, ordered by increasing complexity:
| # | Problem | DP Type | State | Choices |
|---|---|---|---|---|
| 1 | Predict the Winner | Interval DP | (i, j) subarray | Pick left or right |
| 2 | Guess Number Higher Lower II | Decision Tree DP | (i, j) range | Pick pivot k |
| 3 | Optimal Strategy for a Game | Interval DP | (i, j) subarray | Pick left or right |
| 4 | Sum Game | Interval DP | (i, j) subarray | Pick from ends |
| 5 | Can I Win | Bitmask DP | Bitmask of used numbers | Pick unused number |
| 6 | New 21 Game | Probability DP | Current score | Draw or stop |