있다 3n 다양한 크기의 동전 더미, 당신과 당신의 친구들은 다음과 같이 동전 더미를 가져갈 것입니다.
- 각 단계에서 선택하게 됩니다. 어느 삼 동전 더미(반드시 연속적인 것은 아님).
- 당신의 선택에 따라 Alice는 최대 동전 수가 있는 더미를 선택합니다.
- 최대 동전 수로 다음 더미를 선택합니다.
- 친구 Bob이 마지막 더미를 고를 것입니다.
- 더 이상 동전 더미가 없을 때까지 반복합니다.
주어진 정수 배열 말뚝 어디 말뚝(i) 는 동전의 수입니다. i번째 말뚝.
보유할 수 있는 최대 코인 수를 반환합니다.
예 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.
예 2:
Input: piles = (2,4,5)
Output: 4
예 3:
Input: piles = (9,8,7,6,5,1,2,3,4)
Output: 18
제약:
- 3 <= 더미.길이 <= 105
- 더미.길이 % 3 == 0
- 1 <= 말뚝(i) <= 104
class Solution:
def maxCoins(self, piles: List(int)) -> int:
piles=sorted(piles)(len(piles)//3:)
return sum(piles(i) for i in range(0,len(piles),2))