当前位置:网站首页>LeetCode 6006. Take out the least number of magic beans
LeetCode 6006. Take out the least number of magic beans
2022-07-06 00:09:00 【Daylight629】
6006. Take out the smallest number of magic beans
To give you one just An array of integers beans , Each integer represents the number of magic beans in a bag .
Please... From each bag take out Some beans ( It's fine too Don't take out ), Make the rest Non empty In the bag ( namely At least also One Magic bean bag ) Number of magic beans equal . Once the magic beans are removed from the bag , You can't put it in any other bag .
Please return to where you need to take out the magic beans Minimum number .
Example 1:
Input :beans = [4,1,6,5]
Output :4
explain :
- We never had 1 Take it out of a magic bean bag 1 A magic bean .
The number of magic beans left in the bag is :[4,0,6,5]
- Then we have 6 Take it out of a magic bean bag 2 A magic bean .
The number of magic beans left in the bag is :[4,0,4,5]
- Then we have 5 Take it out of a magic bean bag 1 A magic bean .
The number of magic beans left in the bag is :[4,0,4,4]
A total of 1 + 2 + 1 = 4 A magic bean , The number of magic beans left in the non empty bag is equal .
Nothing is better than taking out 4 A plan with fewer magic beans .
Example 2:
Input :beans = [2,10,3,2]
Output :7
explain :
- We never had 2 Take it out of one of the bags of magic beans 2 A magic bean .
The number of magic beans left in the bag is :[0,10,3,2]
- Then we have... From another 2 Take it out of a magic bean bag 2 A magic bean .
The number of magic beans left in the bag is :[0,10,3,0]
- Then we have 3 Take it out of a magic bean bag 3 A magic bean .
The number of magic beans left in the bag is :[0,10,0,0]
A total of 2 + 2 + 3 = 7 A magic bean , The number of magic beans left in the non empty bag is equal .
Nothing is better than taking out 7 A plan with fewer magic beans .
Tips :
1 <= beans.length <= 1051 <= beans[i] <= 105
Two 、 Method 1
Sort + The prefix and
class Solution {
public long minimumRemoval(int[] beans) {
long res = Long.MAX_VALUE;
long sum = 0;
Arrays.sort(beans);
for (int i = 0; i < beans.length; i++) {
sum += beans[i];
}
for (int i = 0; i < beans.length; i++) {
long temp = 0;
temp += sum - (long)(beans.length - i) * beans[i];
res = Math.min(res, temp);
}
return res;
}
}
Complexity analysis
Time complexity :O(nlogn).
Spatial complexity :O(1).
边栏推荐
- There is no network after configuring the agent by capturing packets with Fiddler mobile phones
- QT--线程
- [QT] QT uses qjson to generate JSON files and save them
- Global and Chinese market of digital serial inverter 2022-2028: Research Report on technology, participants, trends, market size and share
- 认识提取与显示梅尔谱图的小实验(观察不同y_axis和x_axis的区别)
- CAS and synchronized knowledge
- NSSA area where OSPF is configured for Huawei equipment
- mysql-全局锁和表锁
- Open3D 点云随机添加噪声
- 如何解决ecology9.0执行导入流程流程产生的问题
猜你喜欢

MySQL之函数

XML配置文件(DTD详细讲解)

MySql——CRUD

权限问题:source .bash_profile permission denied

Open source CRM customer relationship system management system source code, free sharing

Teach you to run uni app with simulator on hbuilderx, conscience teaching!!!

如何解决ecology9.0执行导入流程流程产生的问题

Browser local storage

There is no network after configuring the agent by capturing packets with Fiddler mobile phones

18. (ArcGIS API for JS) ArcGIS API for JS point collection (sketchviewmodel)
随机推荐
MySql——CRUD
Zhongjun group launched electronic contracts to accelerate the digital development of real estate enterprises
Effet Doppler (déplacement de fréquence Doppler)
C # input how many cards are there in each of the four colors.
软件测试工程师必会的银行存款业务,你了解多少?
Determinant learning notes (I)
如何解决ecology9.0执行导入流程流程产生的问题
NSSA area where OSPF is configured for Huawei equipment
Global and Chinese market of valve institutions 2022-2028: Research Report on technology, participants, trends, market size and share
[designmode] adapter pattern
Global and Chinese market of digital serial inverter 2022-2028: Research Report on technology, participants, trends, market size and share
【GYM 102832H】【模板】Combination Lock(二分图博弈)
从底层结构开始学习FPGA----FIFO IP核及其关键参数介绍
Shardingsphere source code analysis
My colleagues quietly told me that flying Book notification can still play like this
Senparc.Weixin.Sample.MP源码剖析
提升工作效率工具:SQL批量生成工具思想
[QT] QT uses qjson to generate JSON files and save them
The global and Chinese markets of dial indicator calipers 2022-2028: Research Report on technology, participants, trends, market size and share
GD32F4xx uIP协议栈移植记录