There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows:
3 piles of coins (not necessarily consecutive).Given an array of integers piles where piles[i] is the number of coins in the ith pile.
Return the maximum number of coins that you can have.
Example 1:
Input: piles = [2,4,1,2,7,8] Output: 9 Explanation: Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal.
Example 2:
Input: piles = [2,4,5] Output: 4
Example 3:
Input: piles = [9,8,7,6,5,1,2,3,4] Output: 18
Constraints:
3 <= piles.length <= 105piles.length % 3 == 01 <= piles[i] <= 104When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for this coin problem means trying every possible combination of taking piles of coins for you, your friend, and the remaining piles for someone else. We'll check which of these combinations gives you the most coins.
Here's how the algorithm would work step-by-step:
def max_coins_brute_force(piles): maximum_coins = 0
# Iterate through all possible combinations for your piles
for i in range(1 << len(piles)):
your_piles = []
friends_piles = []
other_piles = []
current_coins = 0
# Assign piles based on the current combination
for pile_index in range(len(piles)):
# The 'i' variable represents our potential piles
if (i >> pile_index) & 1:
your_piles.append(piles[pile_index])
else:
remaining_piles = piles[:pile_index] + piles[pile_index+1:]
friend_assigned = False
#Assign friends piles
for j in range(1 << len(remaining_piles)):
friends_piles_inner = []
others_piles_inner = []
for remaining_index in range(len(remaining_piles)):
if (j >> remaining_index) & 1:
friends_piles_inner.append(remaining_piles[remaining_index])
else:
others_piles_inner.append(remaining_piles[remaining_index])
#Calculate total for the current combination
current_coins = sum(your_piles)
#Update maximum coins earned
maximum_coins = max(maximum_coins, current_coins)
return maximum_coinsThe best way to maximize your coins is to focus on always picking the best possible pile for yourself. We can do this by focusing on what piles the worst possible picker will take, so we can avoid those piles.
Here's how the algorithm would work step-by-step:
def maximum_number_of_coins_you_can_get(piles): piles.sort()
number_of_piles = len(piles)
my_coins = 0
left_index = 0
right_index = number_of_piles - 1
# We iterate while we have at least 3 piles remaining.
while left_index < right_index:
# We always pick the second largest from the remaining piles
my_coins += piles[right_index - 1]
# Move the left pointer to avoid smallest piles.
left_index += 1
# Decrement right pointer by 2 since other players pick one each
right_index -= 2
return my_coins| Case | How to Handle |
|---|---|
| Null or empty piles array | Return 0 immediately as there are no piles to collect from. |
| piles array with only one or two elements | If the length is 1 or 2, return 0, as you need at least 3 piles to execute a turn. |
| piles array with maximum size (e.g., 10^5) | Ensure the algorithm's time complexity is efficient (O(n log n) or better) to avoid exceeding time limits by using sorting. |
| piles array contains all identical values | The sorting step should correctly handle this case, distributing coins evenly, but ensure the indexing logic is correct after sorting. |
| piles array contains negative numbers | The problem statement doesn't specify this so assume positive numbers but clarify with the interviewer, and if negative, ensure the solution handles negative pile sizes by either throwing an exception or returning 0 as an error condition. |
| piles array contains zero values | The algorithm should function correctly with zero-value piles, as they would just contribute nothing to the total sum. |
| Integer overflow when calculating the sum of coins | Use long long integers for accumulating the sum if the individual pile values or the number of piles are large enough to cause an overflow with regular integers. |
| piles array is already sorted in descending order | The sorting algorithm should still execute correctly, albeit potentially less efficiently, and the coin selection logic should proceed as intended. |