[LeetCode] Maximum Number of Coins You Can Get

1561. Maximum Number of Coins You Can Get

There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows:

  • In each step, you will choose any 3 piles of coins (not necessarily consecutive).
  • Of your choice, Alice will pick the pile with the maximum number of coins.
  • You will pick the next pile with the maximum number of coins.
  • Your friend Bob will pick the last pile.
  • Repeat until there are no more piles of coins.

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.

1
2
3
4
5
6
7
8
9
10
class Solution {
public:
int maxCoins(vector<int>& piles) {
int res = 0, pick = piles.size() / 3;
sort(begin(piles), end(piles));
for(int i = piles.size() - 2; pick; i -= 2, pick -= 1)
res += piles[i];
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/08/18/PS/LeetCode/maximum-number-of-coins-you-can-get/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.