当前位置:网站首页>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 <= 105
1 <= 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).
边栏推荐
- Hudi of data Lake (2): Hudi compilation
- What is information security? What is included? What is the difference with network security?
- Hardware and interface learning summary
- Configuring OSPF load sharing for Huawei devices
- 7.5 装饰器
- Gd32f4xx UIP protocol stack migration record
- "14th five year plan": emphasis on the promotion of electronic contracts, electronic signatures and other applications
- Tools to improve work efficiency: the idea of SQL batch generation tools
- 转:未来,这样的组织才能扛住风险
- The use of El cascader and the solution of error reporting
猜你喜欢
wx. Getlocation (object object) application method, latest version
China Jinmao online electronic signature, accelerating the digitization of real estate business
【二叉搜索树】增删改查功能代码实现
Browser local storage
时区的区别及go语言的time库
总结了 800多个 Kubectl 别名,再也不怕记不住命令了!
Zero rhino technology joined hands with the intelligence Club: the "causal faction" forum was successfully held, and the "causal revolution" brought the next generation of trusted AI
PV静态创建和动态创建
Initialiser votre vecteur & initialisateur avec une liste Introduction à la Liste
Key structure of ffmpeg - avformatcontext
随机推荐
JS can really prohibit constant modification this time!
Online yaml to CSV tool
Add noise randomly to open3d point cloud
Permission problem: source bash_ profile permission denied
Wechat applet -- wxml template syntax (with notes)
FFmpeg学习——核心模块
总结了 800多个 Kubectl 别名,再也不怕记不住命令了!
How much do you know about the bank deposit business that software test engineers must know?
20220703 week race: number of people who know the secret - dynamic rules (problem solution)
跟着CTF-wiki学pwn——ret2libc1
XML configuration file (DTD detailed explanation)
GD32F4xx uIP协议栈移植记录
云呐|公司固定资产管理系统有哪些?
Browser local storage
时区的区别及go语言的time库
XML配置文件(DTD详细讲解)
云呐|固定资产管理系统主要操作流程有哪些
FFT learning notes (I think it is detailed)
QT a simple word document editor
Tools to improve work efficiency: the idea of SQL batch generation tools