#3222
Find the Winning Player in Coin Game
EasyMathSimulationGame TheoryGame TheoryMathematical Simulation
Approaches
Brute ForceOptimal
Complexity Comparison
| Brute Force | Optimal Solution★ | |
|---|---|---|
| Time | O(n²) | O(1) |
| Space | O(1) | O(1) |
💡
Intuition
Time O(1)Space O(1)
In the optimal solution, we realize that the game can be reduced to a simple calculation. The maximum number of turns is determined by the limiting factor of coins available. The player who can make the last move wins, and we can calculate the total turns directly.
⚙️
Algorithm
2 steps- 1Step 1: Calculate the maximum turns possible as min(x, y // 4).
- 2Step 2: If the total turns are odd, Alice wins; if even, Bob wins.
solution.py3 lines
1def find_winner(x, y):
2 turns = min(x, y // 4)
3 return 'Alice' if turns % 2 == 0 else 'Bob'ℹ
Complexity note: The time complexity is O(1) because we are performing a constant number of operations regardless of the input size.
- 1Understanding the game mechanics helps simplify the problem significantly.
- 2Optimal play means calculating the maximum possible turns rather than simulating each turn.
Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.