Maximum Number of Coins You Can Get (LeetCode 1561): Solution and Explanation
Piles are split into triples. Alice always takes the largest pile of a triple, you take the middle one and Bob the smallest. Choose the triples so your share is as large as possible.
Intuition
You never get the largest pile of a triple, so the best you can hope for in each round is the second largest.
Bob's pile is lost to you no matter what, so make it as small as possible. Each round should burn exactly one tiny pile.
That leaves the top two thirds for Alice and you. Pairing the two largest remaining piles each round gives you the second, fourth, sixth largest pile, and so on.
After sorting ascending, those are exactly the piles at indices n, n + 2, n + 4, up to the end.
Approach: Sort and sum alternate piles
- Sort
pilesascending and letn = len(piles) / 3. - Skip the first n piles; they go to Bob.
- Walk from index n to the end in steps of 2 and add each pile to
total. - Return
total.
Step-by-step example
piles = [2,4,1,2,7,8]
- Sorted: [1, 2, 2, 4, 7, 8], n = 2. Bob's third is [1, 2].
- Round 1 uses (2, 2, 4) in code order: you take index 2 (value 2), Alice takes 4, Bob takes 2.
- Round 2 uses (1, 7, 8): you take index 4 (value 7), Alice takes 8, Bob takes 1.
- Your total is 2 + 7 = 9.
Code: Python, Java, C++
1class Solution:
2 def maxCoins(self, piles: List[int]) -> int:
3 piles.sort()
4 n = len(piles) // 3
5 total = 0
6 for i in range(n, len(piles), 2):
7 total += piles[i]
8 return total1class Solution {
2 public int maxCoins(int[] piles) {
3 Arrays.sort(piles);
4 int n = piles.length / 3;
5 int total = 0;
6 for (int i = n; i < piles.length; i += 2) {
7 total += piles[i];
8 }
9 return total;
10 }
11}1class Solution {
2public:
3 int maxCoins(vector<int>& piles) {
4 sort(piles.begin(), piles.end());
5 int n = piles.size() / 3;
6 int total = 0;
7 for (int i = n; i < (int)piles.size(); i += 2) {
8 total += piles[i];
9 }
10 return total;
11 }
12};Time and space complexity
O(n log n)
Sorting 3n piles dominates; the sum is a single pass over 2n of them.
O(1)
Apart from the sort's own memory, only a counter and the total are stored.
Other approaches
Counting sort (time O(n + V), space O(V))
Pile sizes are at most 10^4, so count how many piles have each size and walk the counts from the top, alternately skipping and taking, for n takes.
Deque simulation (time O(n log n), space O(n))
Put the sorted piles in a deque and each round pop two from the back (Alice, then you) and one from the front (Bob). Same result, more bookkeeping.
Common mistakes
- Taking the second largest of the top three consecutive piles and then giving Bob the next one. Bob should always get the smallest remaining pile.
- Starting the loop at index 1 or n - 1 instead of n. Bob's third has exactly n piles.
- Summing every second pile from the top down but running n + 1 or n - 1 times. There are exactly n rounds.
- Worrying about overflow. At most 33,333 piles of 10^4 coins fit easily in a 32-bit int.
Frequently asked questions
Why is giving Bob the smallest piles optimal?
Your pile in a triple is at most the second largest pile overall that is still free. Spending only the smallest piles on Bob keeps every larger pile available for the Alice and you pairs, and an exchange argument shows any other choice can only lower your sum.
Does the order of rounds matter?
No. The set of piles you end up with is the same whichever triple is formed first, so only the sorted positions matter.
Why every second pile starting at n?
Pairing the top 2n piles as (largest, second), (third, fourth), and so on gives you the smaller of each pair. In ascending order those sit at indices n, n + 2, ..., 3n - 2.
What if all piles are equal?
Then every choice is the same and you get n times that value.