[LeetCode] Find the Winning Player in Coin Game

3222. Find the Winning Player in Coin Game

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.

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
string losingPlayer(int x, int y) {
bool res = true;
while(x >= 1 and y >= 4) {
x -= 1, y -= 4;
res = !res;
}
return res ? "Bob" : "Alice";
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2024/07/21/PS/LeetCode/find-the-winning-player-in-coin-game/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.