Maximum Number of Coins You Can Get - Problem

There are 3n piles of coins of varying size. You and your friends Alice and Bob will take piles of coins following this process:

  1. In each step, you choose any 3 piles of coins (not necessarily consecutive)
  2. Alice picks the pile with the maximum number of coins
  3. You pick the pile with the next maximum number of coins
  4. Bob picks the remaining pile
  5. Repeat until no piles remain

Given an array piles where piles[i] is the number of coins in the ith pile, return the maximum number of coins you can collect.

Input & Output

Example 1 — Basic Game
$ Input: piles = [2,4,1,2,7,8]
Output: 9
💡 Note: Sort: [8,7,4,2,2,1]. Take positions 2,3: piles[2]=4, piles[3]=2. Total: 4+2=6. Wait, let me recalculate: n=2, take indices 2 to 3, which gives 4+2=6. But optimal is: Alice gets 8,7 and you get 4,2, Bob gets 2,1. Actually, you get 7+2=9.
Example 2 — Larger Array
$ Input: piles = [9,8,7,6,5,1]
Output: 13
💡 Note: Sort: [9,8,7,6,5,1]. n=2, take indices 2,3: piles[2]=7, piles[3]=6. Total: 7+6=13
Example 3 — Equal Values
$ Input: piles = [1,1,1,1,1,1]
Output: 2
💡 Note: All piles equal, sort doesn't change order. n=2, take indices 2,3: 1+1=2

Constraints

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

Visualization

Tap to expand
Coin Collection Game: Maximize Your CoinsInput: [9,8,7,6,5,1]987651Game Rules:1. Choose any 3 piles2. Alice takes the largest3. You take the middle4. Bob takes the smallestOptimal Strategy:1. Sort: [9,8,7,6,5,1]2. Group: (9,7,5), (8,6,1)3. Alice: 9+8=174. You: 7+6=13 ✓5. Bob: 5+1=6Maximum Coins You Can Get: 13
Understanding the Visualization
1
Input
Array of pile sizes: [9,8,7,6,5,1]
2
Strategy
Sort piles and use greedy selection
3
Output
Maximum coins you can collect: 13
Key Takeaway
🎯 Key Insight: Sort the piles and take strategic middle positions to maximize your share
Asked in
Facebook 15 Google 12 Amazon 8
23.5K Views
Medium Frequency
~15 min Avg. Time
892 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