Class Solution
java.lang.Object
g1501_1600.s1561_maximum_number_of_coins_you_can_get.Solution
1561 - Maximum Number of Coins You Can Get.<p>Medium</p>
<p>There are <code>3n</code> piles of coins of varying size, you and your friends will take piles of coins as follows:</p>
<ul>
<li>In each step, you will choose <strong>any</strong> <code>3</code> piles of coins (not necessarily consecutive).</li>
<li>Of your choice, Alice will pick the pile with the maximum number of coins.</li>
<li>You will pick the next pile with the maximum number of coins.</li>
<li>Your friend Bob will pick the last pile.</li>
<li>Repeat until there are no more piles of coins.</li>
</ul>
<p>Given an array of integers <code>piles</code> where <code>piles[i]</code> is the number of coins in the <code>i<sup>th</sup></code> pile.</p>
<p>Return the maximum number of coins that you can have.</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> piles = [2,4,1,2,7,8]</p>
<p><strong>Output:</strong> 9</p>
<p><strong>Explanation:</strong> Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with <strong>7</strong> coins and Bob the last one.</p>
<p>Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with <strong>2</strong> coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9.</p>
<p>On the other hand if we choose this arrangement (1, <strong>2</strong> , 8), (2, <strong>4</strong> , 7) you only get 2 + 4 = 6 coins which is not optimal.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> piles = [2,4,5]</p>
<p><strong>Output:</strong> 4</p>
<p><strong>Example 3:</strong></p>
<p><strong>Input:</strong> piles = [9,8,7,6,5,1,2,3,4]</p>
<p><strong>Output:</strong> 18</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= piles.length <= 10<sup>5</sup></code></li>
<li><code>piles.length % 3 == 0</code></li>
<li><code>1 <= piles[i] <= 10<sup>4</sup></code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
maxCoins
public int maxCoins(int[] piles)
-