Maximum Number of Coins You Can Get

Updated on 16 June, 2025
Maximum Number of Coins You Can Get header image

Problem Statement

The problem presents a scenario where you and two friends (Alice and Bob) are selecting piles of coins arranged in an array called piles. The number of piles is always a multiple of three (3n), ensuring each of you will always have an equal number of turns. The selection process during each turn involves the following sequence:

  1. Choose any three piles of coins from the array.
  2. Alice will pick the pile containing the maximum number of coins among the chosen three.
  3. You will then pick the pile with the next highest number of coins.
  4. Finally, Bob picks the remaining pile.

This game continues until all the piles are exhausted. The objective for you is to maximize the number of coins collected by strategically choosing triples of piles over the course of the game. Your task is to derive an algorithm or method that returns the maximum number of coins you can collect from any given set of piles.

Examples

Example 1

Input:

piles = [2,4,1,2,7,8]

Output:

9

Explanation:

Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one.
Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one.
The maximum number of coins which you can have are: 7 + 2 = 9.
On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal.

Example 2

Input:

piles = [2,4,5]

Output:

4

Example 3

Input:

piles = [9,8,7,6,5,1,2,3,4]

Output:

18

Constraints

  • 3 <= piles.length <= 105
  • piles.length % 3 == 0
  • 1 <= piles[i] <= 104

Approach and Intuition

The challenge in this problem is to maximize your share of coins while minimizing Alice’s share, since she always takes the pile with the highest number of coins from each chosen set of three piles. Here’s a step-by-step approach to understand this problem:

  1. Sort the Array: The first intuitive step towards an effective strategy is sorting the array in ascending order. This will help us make more structured decisions about which triples to form.

  2. Maximum Selection Strategy:

    • To ensure that Bob always gets the least while Alice still gets the maximum from the chosen triples, one should aim to give Alice the piles from the higher end but not the highest in the triple.
    • By choosing triples strategically from the end of the sorted array, we can maneuver the division such that Bob receives the least valuable coins, Alice receives the most, but critically – you receive the second most valuable coins, which are sufficiently valuable over the course of the game.
  3. Selection Pattern:

    • In the sorted array, consider the last three elements for your first triple. Alice will pick the maximum (last element), Bob picks the minimum (first of the three), and you pick the middle element.
    • Continue this pattern moving backwards through the array to form the subsequent triples. This ensures that each selection is optimal for both minimizing Alice's gain over you and maximizing your total.

The pattern mentioned has a central idea: to distribute the wealth in such a way that Bob consistently receives the least valued coins of each triplet. By sorting the array and cleverly grouping the piles, you optimize the number of coins you gather while minimizing the maximum coins Alice can take, effectively increasing your overall collection in comparison to any random grouping.

Solutions

  • C++
  • Java
  • Python
cpp
class Solution {
public:
    int maxCoins(vector<int>& piles) {
        sort(piles.begin(), piles.end());
        int totalCoins = 0;
        
        for (int i = piles.size() / 3; i < piles.size(); i += 2) {
            totalCoins += piles[i];
        }
        
        return totalCoins;
    }
};

This C++ solution addresses the problem by calculating the maximum number of coins one can obtain, given a set of piles where each pile contains a number of coins. The algorithm works as follows:

  • First, sort the given vector of coins in ascending order to ensure that the selected piles are maximizing the coins count.
  • Initialize a counter for the total number of coins collected.
  • Start iterating through the sorted piles from a third of the way through the list (since two other players pick coins before your turn), and select every second pile thereafter to maximize your share of the coins.
  • For each selected pile in this subset, add the number of coins to your total.
  • Finally, return the cumulative total coins collected.

This method ensures you always take the most valuable coin piles by skipping over the least valuable third (picked by others) and then picking two out of every three piles. The sorted array allows for picking maximum values consistently.

java
class Solution {
    public int maximumCoins(int[] pile) {
        Arrays.sort(pile);
        int total = 0;
        
        for (int j = pile.length / 3; j < pile.length; j += 2) {
            total += pile[j];
        }
        
        return total;
    }
}

This Java solution addresses the problem of maximizing the number of coins you can get. The strategy involves sorting the coin piles and then iterating through them to maximize gains. Here's how the solution works:

  • First, sort the array of coins. This allows you to directly access the largest piles efficiently.
  • Initialize a total counter to accumulate the number of coins.
  • Use a loop starting from one-third the length of the array, continuing to the end, incrementing by two. This specific starting point and increment are chosen to maximize your coin count based on the problem's rules (picking coins from piles strategically).
  • In each iteration, add the coin count from the selected pile to your total.
  • Finally, return the total count of coins collected.

The solution leverages sorting for easy access to the largest elements and arithmetic operations to select the optimal piles as per the conditions described in the problem, ensuring an efficient and straightforward approach to maximizing the coin count.

python
class Solution:
    def maximumCoins(self, piles: List[int]) -> int:
        piles.sort()
        total_coins = 0
        
        for index in range(len(piles) // 3, len(piles), 2):
            total_coins += piles[index]
        
        return total_coins

The Python3 solution provided focuses on maximizing the number of coins one can get from a list of coin piles. The algorithm follows a simple yet effective logic outlined below:

  • First, sort the list of coin piles in increasing order. This arrangement helps in managing the distribution strategy efficiently.
  • Initialize a variable to store the total number of coins collected.
  • Iterate through the sorted list starting from one-third of its length till the end. By selecting every second pile for this segment, it ensures that you're picking up piles while leaving the smallest ones, hence maximizing the coins collected.
  • During each iteration, add the value of the current pile to the total coins collected.
  • Finally, the function returns the total coins collected.

This approach ensures that by avoiding the smallest piles and focusing on the larger ones in the sorted list, one maximizes the total number of coins collected.

Comments

No comments yet.