当前位置:网站首页>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^51 <= 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;
}
};边栏推荐
- Use the difference between "Chmod a + X" and "Chmod 755" [closed] - difference between using "Chmod a + X" and "Chmod 755" [closed]
- Comment mettre en place une équipe technique pour détruire l'entreprise?
- STL container
- Win:使用 Shadow Mode 查看远程用户的桌面会话
- [机缘参悟-38]:鬼谷子-第五飞箝篇 - 警示之一:有一种杀称为“捧杀”
- 220213c language learning diary
- Educational Codeforces Round 122 (Rated for Div. 2) ABC
- How to build a technical team that will bring down the company?
- Introduce reflow & repaint, and how to optimize it?
- phpstrom设置函数注释说明
猜你喜欢

Interesting practice of robot programming 15- autoavoidobstacles

Lsblk command - check the disk of the system. I don't often use this command, but it's still very easy to use. Onion duck, like, collect, pay attention, wait for your arrival!

Yyds dry inventory jetpack hit dependency injection framework Getting Started Guide

Prometheus monitors the correct posture of redis cluster
![[技术发展-26]:新型信息与通信网络的数据安全](/img/13/10c8bd340017c6516edef41cd3bf6f.png)
[技术发展-26]:新型信息与通信网络的数据安全

Icu4c 70 source code download and compilation (win10, vs2022)

Matrixone 0.2.0 is released, and the fastest SQL computing engine is coming

He was laid off.. 39 year old Ali P9, saved 150million

Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool

Practical case of SQL optimization: speed up your database
随机推荐
. Net starts again happy 20th birthday
官宣!第三届云原生编程挑战赛正式启动!
Binary tree traversal - middle order traversal (golang)
力扣剑指offer——二叉树篇
Yyds dry inventory swagger positioning problem ⽅ formula
[understanding of opportunity -38]: Guiguzi - Chapter 5 flying clamp - warning one: there is a kind of killing called "killing"
Incremental backup? db full
es使用collapseBuilder去重和只返回某个字段
Icu4c 70 source code download and compilation (win10, vs2022)
Win:使用 PowerShell 检查无线信号的强弱
Timescaledb 2.5.2 release, time series database based on PostgreSQL
Five ways to query MySQL field comments!
Advanced learning of MySQL -- Application -- Introduction
Tla+ through examples (XI) -- propositional logic and examples
Data guard -- theoretical explanation (III)
85.4% mIOU! NVIDIA: using multi-scale attention for semantic segmentation, the code is open source!
Yolov5 model training and detection
Luo Gu Pardon prisoners of war
Using druid to connect to MySQL database reports the wrong type
How to build a technical team that will bring down the company?