Problem

You are given two positive integers x and y, denoting the number of coins with values 75 and 10 respectively.

Alice and Bob are playing a game. Each turn, starting with Alice, the player must pick up coins with a total value 115. If the player is unable to do so, they lose the game.

Return the name of the player who wins the game if both players play optimally.

https://leetcode.cn/problems/find-the-winning-player-in-coin-game/

Example 1:

Input: x = 2, y = 7
Output: "Alice"
Explanation:
The game ends in a single turn:
Alice picks 1 coin with a value of 75 and 4 coins with a value of 10.

Example 2:

Input: x = 4, y = 11
Output: "Bob"
Explanation:
The game ends in 2 turns:
Alice picks 1 coin with a value of 75 and 4 coins with a value of 10.
Bob picks 1 coin with a value of 75 and 4 coins with a value of 10.

Constraints:

  • 1 <= x, y <= 100

Test Cases

1
2
class Solution:
def winningPlayer(self, x: int, y: int) -> str:
solution_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
import pytest

from solution import Solution


@pytest.mark.parametrize('x, y, expected', [
(2, 7, 'Alice'),
(4, 11, 'Bob'),
])
class Test:
def test_solution(self, x, y, expected):
sol = Solution()
assert sol.winningPlayer(x, y) == expected

Thoughts

因为 105 = 75 * 1 + 10 * 4 是唯一的拿硬币方案,所以看二元组 (x, y) 除以 (1, 4) 的整数部分,是几就表示可以拿几次硬币。如果次数是奇数则 Alice 赢,否则 Bob 硬。

(x, y) // (1, 4) = min(x // 1, y // 4)

Code

solution.py
1
2
3
4
5
6
7
class Solution:
def winningPlayer(self, x: int, y: int) -> str:
n = y >> 2
if x < n:
n = x

return 'Alice' if n & 1 else 'Bob'