当前位置:网站首页>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;
}
};
边栏推荐
- Go RPC call
- Outlook: always prompt for user password
- Flutter 2.10 update details
- Incremental backup? db full
- 85.4% mIOU! NVIDIA: using multi-scale attention for semantic segmentation, the code is open source!
- Security level
- Yolov5 model training and detection
- Es uses collapsebuilder to de duplicate and return only a certain field
- Interpretation of mask RCNN paper
- Exploration and practice of integration of streaming and wholesale in jd.com
猜你喜欢
Introduce reflow & repaint, and how to optimize it?
STM32 series - serial port UART software pin internal pull-up or external resistance pull-up - cause problem search
Visual explanation of Newton iteration method
One plus six brushes into Kali nethunter
Win:使用 PowerShell 检查无线信号的强弱
Practice of tdengine in TCL air conditioning energy management platform
Interesting practice of robot programming 16 synchronous positioning and map building (SLAM)
Yyds dry inventory swagger positioning problem ⽅ formula
Exploration and Practice of Stream Batch Integration in JD
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!
随机推荐
Can financial products be redeemed in advance?
phpstrom设置函数注释说明
Introduce reflow & repaint, and how to optimize it?
Restful fast request 2022.2.1 release, support curl import
Serious bugs with lifted/nullable conversions from int, allowing conversion from decimal
Using druid to connect to MySQL database reports the wrong type
[understanding of opportunity -38]: Guiguzi - Chapter 5 flying clamp - warning one: there is a kind of killing called "killing"
Interesting practice of robot programming 14 robot 3D simulation (gazebo+turtlebot3)
Interesting practice of robot programming 15- autoavoidobstacles
Vulnstack3
STM32 series - serial port UART software pin internal pull-up or external resistance pull-up - cause problem search
Rabbit MQ message sending of vertx
A tab Sina navigation bar
Codeforces Global Round 19 ABC
Is there a sudden failure on the line? How to make emergency diagnosis, troubleshooting and recovery
[technology development-26]: data security of new information and communication networks
One plus six brushes into Kali nethunter
JVM - when multiple threads initialize the same class, only one thread is allowed to initialize
流批一體在京東的探索與實踐
Subject 3 how to turn on the high beam diagram? Is the high beam of section 3 up or down