3222. Find the Winning Player in Coin Game LeetCode Solution

In this guide, you will get 3222. Find the Winning Player in Coin Game LeetCode Solution with the best time and space complexity. The solution to Find the Winning Player in Coin Game problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Find the Winning Player in Coin Game solution in C++
  4. Find the Winning Player in Coin Game solution in Java
  5. Find the Winning Player in Coin Game solution in Python
  6. Additional Resources
3222. Find the Winning Player in Coin Game LeetCode Solution image

Problem Statement of 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.

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.

See also  1937. Maximum Number of Points with Cost LeetCode Solution

Constraints:

1 <= x, y <= 100

Complexity Analysis

  • Time Complexity: O(1)
  • Space Complexity: O(1)

3222. Find the Winning Player in Coin Game LeetCode Solution in C++

class Solution {
 public:
  string losingPlayer(int x, int y) {
    return min(x, y / 4) % 2 == 0 ? "Bob" : "Alice";
  }
};
/* code provided by PROGIEZ */

3222. Find the Winning Player in Coin Game LeetCode Solution in Java

class Solution {
  public String losingPlayer(int x, int y) {
    return Math.min(x, y / 4) % 2 == 0 ? "Bob" : "Alice";
  }
}
// code provided by PROGIEZ

3222. Find the Winning Player in Coin Game LeetCode Solution in Python

class Solution:
  def losingPlayer(self, x: int, y: int) -> str:
    return 'Bob' if min(x, y // 4) % 2 == 0 else 'Alice'
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.