Skip to main content
LeetCode 375, Medium. Topics: Math, Dynamic Programming, Minimax, Game Theory. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 28 parametrized pytest cases, and a playground notebook:

Problem

We are playing the Guessing Game. The game will work as follows: <ol> <li>I pick a number between <code>1</code> and <code>n</code>.</li> <li>You guess a number.</li> <li>If you guess the right number, <strong>you win the game</strong>.</li> <li>If you guess the wrong number, then I will tell you whether the number I picked is <strong>higher or lower</strong>, and you will continue guessing.</li> <li>Every time you guess a wrong number <code>x</code>, you will pay <code>x</code> dollars. If you run out of money, <strong>you lose the game</strong>.</li> </ol> Given a particular <code>n</code>, return <em>the minimum amount of money you need to <strong>guarantee a win regardless of what number I pick</strong></em>.

Examples

Example 1

Constraints

  • 1 <= n <= 200

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

Last modified on September 7, 2026