Maximum Coins You Can Collect
You are given an integer array piles, where piles[i] represents the number of coins in the ith pile.
There are exactly 3n piles, and in each round, you must select any 3 piles.
Once the 3 piles are chosen:
- Alice takes the pile with the maximum number of coins.
- You take the pile with the second maximum number of coins.
- Bob takes the remaining pile.
This process continues until all piles are used exactly once.
Return the maximum number of coins you can collect.
Example 1:
Input: piles = [1,2,3,4,5,6]
Output: 8
Explanation:
One optimal way is:
Choose piles (1, 5, 6)
- Alice takes 6
- You take 5
- Bob takes 1
Choose piles (2, 3, 4)
- Alice takes 4
- You take 3
- Bob takes 2
So, the total number of coins you collect is 5 + 3 = 8.
Example 2:
Input: piles = [2,2,3,3,4,4]
Output: 7
Explanation:
One optimal selection is:
Choose piles (2, 4, 4)
- Alice takes 4
- You take 4
- Bob takes 2
Choose piles (2, 3, 3)
- Alice takes 3
- You take 3
- Bob takes 2
So, the maximum coins you can collect are 4 + 3 = 7.
Now Your Turn!
Pick the correct output for the given inputInput: piles = [3,4,5,1,2,6]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 3 <= piles.length <= 105
- piles.length % 3 == 0
- 1 <= piles[i] <= 104