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:
- In each step, you choose any 3 piles of coins (not necessarily consecutive)
- Alice picks the pile with the maximum number of coins
- You pick the pile with the next maximum number of coins
- Bob picks the remaining pile
- 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
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code