Maximum Number of Coins You Can Get
Share piles of coins with two friends, three piles at a time, and find the most coins you can keep.
You are given an array piles
Example 1
Output: 9
Explanation: choose 1, 7 and 8 first, and you get 7. Then 2, 2 and 4 are left, and you get 2.
Example 2
Output: 4
Example 3
Output: 18
The Idea
Picture nine piles of coins on a table, with 1 to 9 coins in them, as in Example 3. For each round you choose three of them. Alice grabs the biggest of the three, you take the middle one, and Bob takes the smallest. Which three piles should each round take?
Taking the three biggest piles first feels natural. The rounds are then 9, 8 and 7, next 6, 5 and 4, and last 3, 2 and 1. You get 8 + 5 + 2 = 15. The answer is 18, so something is lost. Look at what Bob gets: 7 and 4, two fairly big piles that you never see.
Bob takes one pile in every round, whatever you choose, so with nine piles he gets three of them. The best you can do is hand him the three smallest. Alice cannot be avoided either. Your biggest pile always has a bigger Alice pile in its round, so at best it is the second biggest on the table. Your second biggest pile has at least three bigger ones: your biggest pile, Alice's pile in that same round, and Alice's pile in its own round. So at best it is the fourth biggest, and in the same way your third pile is at best the sixth biggest.
One simple rule reaches all of these best cases: in every round, take the two biggest piles left and the smallest pile left. Look at the picture below, which shows Example 1 with its piles sorted from smallest to biggest.
Round 1 takes the two biggest piles, 7 and 8, with the smallest pile, 1. Alice gets 8 and you get 7. Round 2 takes what is left, 2, 2 and 4, and you get 2, for 9 in total. Now read the top row: Bob's two piles are the first two, and after them the piles alternate between you and Alice. Your piles are the second and fourth biggest, the best you could hope for.
The rule picks what looks best in each round and never goes back to change an earlier round. A method that works like that is called a greedy algorithm. Greedy choices are not always right, but here the best cases above show that nothing can beat them.
Solution Approach
Two things need settling: how to form the rounds quickly, and how to find your piles without playing the rounds one by one.
Forming the rounds
Why not try every way of splitting the piles into rounds? The number of ways grows very fast. Nine piles can already be split into groups of three in 280 ways, and thirty piles in more than 1018 ways. The greedy rule needs only the smallest and the biggest piles left, and sorting the array once puts them at its two ends.
Finding your piles in the sorted array
Where are your piles once the array is sorted? Bob gets a third of the piles, the smallest ones, so they fill the positions from 0 up to just before piles.length / 3piles.length / 3
Java Solution
The method maxCoins
- First, sort the piles from smallest to biggest with
.Arrays.sort(piles) - Set
, your running total, to 0.coins - A loop moves
fromi , the first position after Bob's piles, to the end of the array. It steps by 2, so it lands on your piles and skips Alice's. Each step addspiles.length / 3 topiles[i] .coins - When the loop ends, return
.coins
The main
import java.util.Arrays;
public class MaximumNumberOfCoinsYouCanGet {
public static int maxCoins(int[] piles) {
Arrays.sort(piles);
int coins = 0;
// the smallest third goes to Bob; from the rest you take every second pile
for (int i = piles.length / 3; i < piles.length; i += 2) {
coins += piles[i];
}
return coins;
}
public static void main(String[] args) {
System.out.println(maxCoins(new int[] {2, 4, 1, 2, 7, 8}));
System.out.println(maxCoins(new int[] {2, 4, 5}));
System.out.println(maxCoins(new int[] {9, 8, 7, 6, 5, 1, 2, 3, 4}));
System.out.println(maxCoins(new int[] {5, 5, 5, 5, 5, 5}));
}
}
Running it prints:
9
4
18
10
The 18 on the third line is three more than the 15 that the biggest-piles-first rounds gave in The Idea. With the sorted piles 1 to 9, the loop starts at position 3 and adds 4, 6 and 8, instead of 8, 5 and 2.
The last line, 10, comes from the six equal piles. You get two piles of 5, and the ties make no difference: whichever of two equal piles Alice takes, the one left for you holds the same coins.
Complexity
The work is the sort, plus one short pass over your piles.
- Time: O(n log n), because sorting the n piles takes O(n log n), and the loop that follows makes only n / 3 additions.
- Space: O(1) besides the sort. The method keeps one total and one position, and
sorts anArrays.sort array in place, using only a little stack space while it works.int
Sorting first and then pairing piles from the two ends also solves