[코테 풀이] Maximum Number of Coins You Can Get

시내·2024년 6월 26일

Q_1561) Maximum Number of Coins You Can Get

출처 : https://leetcode.com/problems/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.

class Solution {
    public int maxCoins(int[] piles) {
        ArrayList<Integer> arrayList = new ArrayList<>();
        for (int p : piles) arrayList.add(p);
        int answer = 0;
        Collections.sort(arrayList);
        
        while (arrayList.size() > 0) {
            int alice = arrayList.size() - 1;
            arrayList.remove(alice);

            int me = arrayList.size() - 1;
            answer += arrayList.get(me);
            arrayList.remove(me);

            arrayList.remove(0);
        }
        return answer;
    }
}
profile
contact 📨 ksw08215@gmail.com

0개의 댓글