当前位置:网站首页>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;
}
};
边栏推荐
- Logstash、Fluentd、Fluent Bit、Vector? How to choose the appropriate open source log collector
- Unified blog writing environment
- MATLB|多微电网及分布式能源交易
- Tla+ through examples (XI) -- propositional logic and examples
- 187. Repeated DNA sequence - with unordered_ Map basic content
- Win: enable and disable USB drives using group policy
- Unpool(nn.MaxUnpool2d)
- Win: use shadow mode to view the Desktop Session of a remote user
- Main window in QT application
- Win: use PowerShell to check the strength of wireless signal
猜你喜欢
"2022" is a must know web security interview question for job hopping
Mysql database | build master-slave instances of mysql-8.0 or above based on docker
Official announcement! The third cloud native programming challenge is officially launched!
Exploration and practice of integration of streaming and wholesale in jd.com
Missile interception -- UPC winter vacation training match
如何搭建一支搞垮公司的技術團隊?
线上故障突突突?如何紧急诊断、排查与恢复
R语言用logistic逻辑回归和AFRIMA、ARIMA时间序列模型预测世界人口
Codeforces Global Round 19 ABC
One plus six brushes into Kali nethunter
随机推荐
【附源码】基于知识图谱的智能推荐系统-Sylvie小兔
Abacus mental arithmetic test
[understanding of opportunity -38]: Guiguzi - Chapter 5 flying clamp - warning one: there is a kind of killing called "killing"
如何搭建一支搞垮公司的技術團隊?
Educational Codeforces Round 122 (Rated for Div. 2) ABC
Application and Optimization Practice of redis in vivo push platform
Comment mettre en place une équipe technique pour détruire l'entreprise?
PHP 约瑟夫环问题
runc hang 导致 Kubernetes 节点 NotReady
220213c language learning diary
Valentine's Day flirting with girls to force a small way, one can learn
phpstrom设置函数注释说明
PHP Joseph Ring problem
Icu4c 70 source code download and compilation (win10, vs2022)
Practical case of SQL optimization: speed up your database
Restful Fast Request 2022.2.1发布,支持cURL导入
JVM's responsibility - load and run bytecode
One plus six brushes into Kali nethunter
Three properties that a good homomorphic encryption should satisfy
A label making navigation bar