Facebook Pixel

3222. Find the Winning Player in Coin Game

EasyMathGame TheorySimulation
Leetcode Link

Problem Description

You are given two positive integers x and y, representing the number of coins with values 75 and 10 respectively. Alice and Bob are engaged in a game where, in each turn, starting with Alice, the current player must pick up coins with a total value of 115. If a player is unable to do so, they lose the game. The task is to determine the winner of the game when both players play optimally.

Intuition

The key to this problem lies in understanding the requirements for picking up coins with a total value of 115. Since 75 and 10 are the only values available, an optimal strategy needs to be developed around these numbers.

Each move requires selecting 115 in total value, which can be efficiently achieved by choosing 2 coins of 75 (which equals 150) and 8 coins of 10 (which also equals 80). Though it sums up over the required total, it ensures that each player is able to pick in valid units that add to exactly 115 by only varying quantities. Consequently, two coins of 75 (which is the main contributing factor of fulfilling the requirement) and eight coins of 10 is the optimal setting due to close to the next highest necessity.

Following this, determine how many such complete sets (of 2 x 75s and 8 x 10s) can be made from the given x and y using k = min(x // 2, y // 8). Once k is determined and consequently each used up coin is discounted equivalently from x and y, reassess the remaining coins:

  • If there are coins left and y is still greater than or equal to 4, Alice can proceed to play another move, implying Bob will lose.
  • Otherwise, Bob takes the win. The strategy for optimizing play unfolds around this critical insight.

By following the optimal path as dictated above, it's possible to ascertain the winner of this intriguing coin-picking game.

Learn more about Math patterns.

Solution Approach

The solution revolves around determining the maximum number of complete sets of coins that meet the value requirement of 115. Here's the approach step-by-step, as reflected in the reference solution:

  1. Calculate Maximum Complete Sets:

    • We want to find how many times we can form the combination that sums to the required value using the equation 2 * 75 + 8 * 10 = 230 to represent overage coherently but logically categorized.
    • Determine k, which is the maximum number of complete sets that can be formed using the formula k = min(x // 2, y // 8). This indicates the limiting factor based on the given quantities of coin types.
  2. Update Remaining Coins:

    • After calculating k, update the remaining number of each type of coin:
      • Decrease x by k * 2 and y by k * 8, representing the coins used up in forming k complete sets.
  3. Determine the Winner:

    • After forming as many complete sets as possible, check if Alice can make another move:
      • If x > 0 and y >= 4, then Alice has enough resources to pick more coins, thus Bob loses and Alice wins.
      • Otherwise, if it's not possible for Alice to satisfy the game's requirements with the remaining coins, Bob wins instead.

This logic is efficiently captured in the function:

class Solution:
    def losingPlayer(self, x: int, y: int) -> str:
        k = min(x // 2, y // 8)
        x -= k * 2
        y -= k * 8
        return "Alice" if x and y >= 4 else "Bob"

Here, the solution takes constant time since it involves a few mathematical calculations and conditional checks. This makes the approach both optimal and straightforward.

Ready to land your dream job?

Unlock your dream job with a 2-minute evaluator for a personalized learning plan!

Start Evaluator

Example Walkthrough

Let's walk through a simple example using the solution approach to understand the game's dynamics:

Suppose we have 5 coins with values of 75 (x = 5) and 16 coins with values of 10 (y = 16). The goal is to determine the winner between Alice and Bob.

1. Calculate Maximum Complete Sets:

  • First, determine the number of complete sets of coins that can sum to 115 using k = min(x // 2, y // 8).
  • Calculate x // 2 = 5 // 2 = 2.
  • Calculate y // 8 = 16 // 8 = 2.
  • Therefore, k = min(2, 2) = 2.

2. Update Remaining Coins:

  • Compute the updated number of coin values after forming k complete sets:
    • Subtract k * 2 from x: x = 5 - (2 * 2) = 1.
    • Subtract k * 8 from y: y = 16 - (2 * 8) = 0.

3. Determine the Winner:

  • With the updated values x = 1 and y = 0, check if Alice can make another move:
    • Alice needs at least x > 0 and y >= 4 to potentially pick coins, but since y < 4, she cannot.
  • Hence, Bob wins the game.

Here, the step-by-step decision making shows how each player can optimize their choices, and the remaining coins indicate whether Alice or Bob comes out as the winner. In this scenario, Bob emerges victorious because Alice lacks the resources to continue the game.

Solution Implementation

1class Solution:
2    def losingPlayer(self, x: int, y: int) -> str:
3        # Calculate the maximum number of operations that can be performed
4        # Both x and y are reduced in each operation: 2 units from x, 8 units from y
5        k = min(x // 2, y // 8)
6      
7        # Update x and y after performing k operations
8        x -= k * 2
9        y -= k * 8
10      
11        # Determine the loser based on remaining x and y
12        # "Alice" loses if there are no moves left or not enough y remaining;
13        # otherwise, "Bob" loses.
14        return "Alice" if x and y >= 4 else "Bob"
15
1class Solution {
2
3    /**
4     * Determines the losing player based on the values of x and y.
5     * 
6     * @param x the number of a certain resource type.
7     * @param y the number of another resource type.
8     * @return the name of the losing player ("Alice" or "Bob").
9     */
10    public String losingPlayer(int x, int y) {
11        // Calculate k based on the minimum possible operation count.
12        int k = Math.min(x / 2, y / 8);
13      
14        // Reduce x and y resources by the corresponding amount used up in k operations.
15        x -= k * 2;
16        y -= k * 8;
17      
18        // Determine the loser based on remaining resources. 
19        // Alice loses if there are still resources to satisfy her condition; otherwise, Bob loses.
20        return (x > 0 && y >= 4) ? "Alice" : "Bob";
21    }
22}
23
1class Solution {
2public:
3    string losingPlayer(int x, int y) {
4        // Determine the maximum possible value of k that satisfies both conditions
5        int k = std::min(x / 2, y / 8);
6
7        // Update the values of x and y after removing k elements
8        x -= k * 2;
9        y -= k * 8;
10
11        // Check the remaining values of x and y to determine the losing player
12        // If x is non-zero and y is greater than or equal to 4, Alice loses; otherwise, Bob loses
13        return (x != 0 && y >= 4) ? "Alice" : "Bob";
14    }
15};
16
1// Function to determine the losing player based on the given rules
2function losingPlayer(x: number, y: number): string {
3    // Calculate the maximum possible value of 'k' based on given conditions
4    // 'k' is the maximum number of complete sets that can be deducted from x and y
5    const k = Math.min(Math.floor(x / 2), Math.floor(y / 8));
6
7    // Reduce 'x' and 'y' by the number of complete sets (k) found
8    x -= k * 2;
9    y -= k * 8;
10
11    // Determine the loser based on remaining values of 'x' and 'y'
12    // If x is still greater than 0 and y is at least 4, Alice loses; otherwise, Bob loses
13    return x > 0 && y >= 4 ? 'Alice' : 'Bob';
14}
15

Time and Space Complexity

The time complexity of the code is O(1) because the operations performed (such as the division, multiplication, subtraction, and comparison) are constant time operations. There are no loops or recursive calls that depend on the size of any input, ensuring that the time taken is constant regardless of the input values x and y.

The space complexity is O(1) because the algorithm uses a fixed amount of additional space. Only a few integer variables (k, x, y) are used, and their space requirements do not change with the size of the input.

Learn more about how to find time and space complexity quickly.


Discover Your Strengths and Weaknesses: Take Our 2-Minute Quiz to Tailor Your Study Plan:
Question 1 out of 10

Problem: Given a list of tasks and a list of requirements, compute a sequence of tasks that can be performed, such that we complete every task once while satisfying all the requirements.

Which of the following method should we use to solve this problem?


Recommended Readings

Want a Structured Path to Master System Design Too? Don’t Miss This!


Load More