1561. 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 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 which you can have.
在这里插入图片描述

思路

  1. 首先总结题目规律,自己要拿的那一份一定要是每一组的中间那份。
  2. 那么要最大化总量,就要把数组排序,每次的三份都是两个最大的加一个最小的。
  3. 思路理清楚了实现就很简单了。

代码实现

class Solution {
    public int maxCoins(int[] piles) {
        Arrays.sort(piles);
        int n = piles.length, sum = 0;
        for (int i = n / 3; i < n; i += 2) {
            sum += piles[i];
        }
        return sum;
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值