当前位置:网站首页>Leetcode takes out the least number of magic beans
Leetcode takes out the least number of magic beans
2022-07-05 02:05:00 【I'm busy 2010】
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 <= 10^5
1 <= beans[i] <= 10^5
C++
class Solution {
public:
long long minimumRemoval(vector<int>& beans) {
long long sum=0;
int n=beans.size();
map<long,long> mp;
for(int i=0;i<n;i++) {
sum+=beans[i];
mp[beans[i]]++;
}
long long res=sum;
long long pre=0;
long long num=n;
for(auto it:mp) {
num-=it.second;
sum-=it.second*it.first;
res=min(res,sum-num*it.first+pre); // Based on the current quantity , The big one is flattened , Small discard
pre+=it.second*it.first;
}
return res;
}
};
边栏推荐
- R language uses logistic regression and afrima, ARIMA time series models to predict world population
- Application and Optimization Practice of redis in vivo push platform
- How to make a cool ink screen electronic clock?
- Practical case of SQL optimization: speed up your database
- Grpc message sending of vertx
- 179. Maximum number - sort
- 增量备份 ?db full
- Prometheus monitors the correct posture of redis cluster
- Interesting practice of robot programming 16 synchronous positioning and map building (SLAM)
- RichView TRVUnits 图像显示单位
猜你喜欢
What sparks can applet container technology collide with IOT
Phpstrom setting function annotation description
Traditional chips and AI chips
[technology development-26]: data security of new information and communication networks
MySQL backup and recovery + experiment
[OpenGL learning notes 8] texture
phpstrom设置函数注释说明
Do you know the eight signs of a team becoming agile?
【LeetCode】88. Merge two ordered arrays
The steering wheel can be turned for one and a half turns. Is there any difference between it and two turns
随机推荐
Exploration and practice of integration of streaming and wholesale in jd.com
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
[illumination du destin - 38]: Ghost Valley - chapitre 5 Flying clamp - one of the Warnings: There is a kind of killing called "hold Kill"
pytorch fine-tuning (funtune) : 镂空设计or 偷梁换柱
[technology development-26]: data security of new information and communication networks
Pytorch register_ Hook (operate on gradient grad)
流批一体在京东的探索与实践
He was laid off.. 39 year old Ali P9, saved 150million
[swagger]-swagger learning
MySQL backup and recovery + experiment
Talk about the things that must be paid attention to when interviewing programmers
Is there a sudden failure on the line? How to make emergency diagnosis, troubleshooting and recovery
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
Win: use shadow mode to view the Desktop Session of a remote user
Yolov5 model training and detection
PHP Joseph Ring problem
C语音常用的位运算技巧
Comment mettre en place une équipe technique pour détruire l'entreprise?
Interesting practice of robot programming 16 synchronous positioning and map building (SLAM)