Maximum Number of Coins You Can Get - Problem
Coin Collection Game - Strategic Optimization

You're playing an exciting coin collection game with your friends Alice and Bob! ๐Ÿช™

Game Rules:
โ€ข There are 3n piles of coins with different amounts
โ€ข In each round, you choose any 3 piles
โ€ข Alice (being greedy) always takes the pile with the most coins
โ€ข You get the pile with the second-most coins
โ€ข Poor Bob gets stuck with the smallest pile
โ€ข This continues until all piles are taken

Your Mission: Choose the 3-pile combinations strategically to maximize the total coins you collect!

Input: An array piles where piles[i] represents the number of coins in pile i
Output: The maximum number of coins you can obtain

Example: With piles [2,4,1,2,7,8], you can get 9 coins by smart selection!

Input & Output

example_1.py โ€” Basic Game
$ Input: piles = [2,4,1,2,7,8]
โ€บ Output: 9
๐Ÿ’ก Note: Choose triplets optimally: (8,7,1) gives you 7, (4,2,2) gives you 2. Total = 7+2 = 9 coins. Alice gets 8+4=12, Bob gets 1+2=3.
example_2.py โ€” Larger Array
$ Input: piles = [9,8,7,6,5,1,2,3,4]
โ€บ Output: 18
๐Ÿ’ก Note: After sorting: [9,8,7,6,5,4,3,2,1]. Form triplets (9,8,1), (7,6,2), (5,4,3). You get positions 1,3,5: 8+6+4 = 18 coins.
example_3.py โ€” Minimum Case
$ Input: piles = [1,100,3]
โ€บ Output: 3
๐Ÿ’ก Note: Only one triplet possible: (100,3,1). Alice takes 100, you get 3, Bob gets 1. Your total is 3 coins.

Visualization

Tap to expand
๐Ÿ† Strategic Treasure Hunt GameGame Setup: Choose 3 treasure chests at a timeAlice(Strongest)YOU(Strategic)Bob(Gets Remainder)Optimal Strategy VisualizationSorted Treasure Chests (by value):100Alice90YOU80Alice70YOU60Alice50YOU30Bob20Bob10BobTriplet Formation:1009010You get: 90807020You get: 70605030You get: 50๐ŸŽฏ Final ResultYour Total: 90 + 70 + 50 = 210Pattern: positions 1, 3, 5... from sorted array
Understanding the Visualization
1
Sort All Treasures
Arrange all treasure chests from most valuable to least valuable
2
Identify Your Targets
Your optimal chests are at positions 1, 3, 5... (every second position)
3
Strategic Pairing
Pair each of your targets with the best available chest (positions 0, 2, 4...)
4
Sacrifice Strategy
Let the weakest player take the smallest chests to maximize your total
Key Takeaway
๐ŸŽฏ Key Insight: Since Alice always takes the maximum pile from each triplet, pair your desired piles (second-largest) with the absolute best piles available. This greedy strategy after sorting guarantees optimal results!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O((3n)!/(3!)^n)

We need to generate all ways to partition 3n items into groups of 3, which is multinomial coefficient

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Space for recursion stack and storing current partition

n
2n
โšก Linearithmic Space

Constraints

  • 3 โ‰ค piles.length โ‰ค 105
  • piles.length % 3 == 0
  • 1 โ‰ค piles[i] โ‰ค 104
Asked in
Google 15 Amazon 12 Apple 8 Microsoft 6
47.2K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen