当前位置:网站首页>LeetCode 1561. The maximum number of coins you can get
LeetCode 1561. The maximum number of coins you can get
2022-07-06 16:42:00 【Daylight629】
1561. The maximum number of coins you can get
Yes 3n A pile of coins in different numbers , You and your friends are going to divide coins in the following way :
- In every round , You will choose arbitrarily 3 Pile coins ( Not necessarily continuous ).
- Alice The pile with the largest number of coins will be taken away .
- You will take away the pile with the second largest number of coins .
- Bob Will take the last pile .
- Repeat the process , Until there are no more coins .
Give you an array of integers piles , among piles[i] It's No i The number of coins in the pile .
Returns the maximum number of coins you can get .
Example 1:
Input :piles = [2,4,1,2,7,8]
Output :9
explain : elect (2, 7, 8) ,Alice Take away 8 The pile of coins , You take it 7 The pile of coins ,Bob Take the last pile .
elect (1, 2, 4) , Alice Take away 4 The pile of coins , You take it 2 The pile of coins ,Bob Take the last pile .
The maximum number of coins you can get :7 + 2 = 9.
Consider the other case , If you choose (1, 2, 8) and (2, 4, 7) , You can only get 2 + 4 = 6 Coin , This is not the optimal solution .
Example 2:
Input :piles = [2,4,5]
Output :4
Example 3:
Input :piles = [9,8,7,6,5,1,2,3,4]
Output :18
Tips :
3 <= piles.length <= 10^5piles.length % 3 == 01 <= piles[i] <= 10^4
Two 、 Method 1
greedy , By sorting , Ahead 1/3 All are Bob Of , We reverse the order , Take the second largest pile
class Solution {
public int maxCoins(int[] piles) {
Arrays.sort(piles);
int round = piles.length / 3;
int idx = piles.length - 2;
int res = 0;
for (int i = 0; i < round; i++) {
res += piles[idx];
idx -= 2;
}
return res;
}
}
Complexity analysis
Time complexity :O(nlogn), among nn Is the length of the array divided by 3( That is, the length of the array is 3n). The time complexity of sorting is O(3nlog3n)=O(3n(log3+logn))=O(nlogn), The time complexity of traversing the array to calculate the maximum number of coins is O(n), So the total time complexity is O(nlogn).
Spatial complexity :O(logn).
边栏推荐
- (lightoj - 1354) IP checking (Analog)
- 解决Intel12代酷睿CPU单线程只给小核运行的问题
- Spark独立集群Worker和Executor的概念
- Research Report on market supply and demand and strategy of Chinese table lamp industry
- Summary of FTP function implemented by qnetworkaccessmanager
- Spark独立集群动态上线下线Worker节点
- Sublime text code formatting operation
- Spark independent cluster dynamic online and offline worker node
- MariaDB的安装与配置
- Research Report on market supply and demand and strategy of double drum magnetic separator industry in China
猜你喜欢

简单尝试DeepFaceLab(DeepFake)的新AMP模型

顺丰科技智慧物流校园技术挑战赛(无t4)

Gridhome, a static site generator that novices must know
![Story of [Kun Jintong]: talk about Chinese character coding and common character sets](/img/d5/9a9e3a0ba57328749d80ec71cb9467.png)
Story of [Kun Jintong]: talk about Chinese character coding and common character sets

Discussion on QWidget code setting style sheet

QT style settings of qcobobox controls (rounded corners, drop-down boxes, up expansion, editable, internal layout, etc.)

QT realizes window topping, topping state switching, and multi window topping priority relationship

第5章 NameNode和SecondaryNameNode

SQL快速入门

Remove the border when input is focused
随机推荐
Research Report of desktop clinical chemical analyzer industry - market status analysis and development prospect prediction
sublime text 代码格式化操作
Study notes of Tutu - process
Research Report on hearing health care equipment industry - market status analysis and development prospect prediction
Raspberry pie 4b64 bit system installation miniconda (it took a few days to finally solve it)
Problem - 1646C. Factorials and Powers of Two - Codeforces
第一章 MapReduce概述
图像处理一百题(11-20)
第2章 HFDS的Shell操作
Spark的RDD(弹性分布式数据集)返回大结果集
Date plus 1 day
Classic application of stack -- bracket matching problem
Pull branch failed, fatal: 'origin/xxx' is not a commit and a branch 'xxx' cannot be created from it
Cmake Express
QT implementation window gradually disappears qpropertyanimation+ progress bar
字节跳动新程序员成长秘诀:那些闪闪发光的宝藏mentor们
Chapter 2 shell operation of hfds
(lightoj - 1349) Aladdin and the optimal invitation (greed)
Mp4 format details
Chapter 6 datanode