当前位置:网站首页>6006. Take out the minimum number of magic beans
6006. Take out the minimum number of magic beans
2022-07-03 20:02:00 【_ Cauchy】
6006. Take out the smallest number of magic beans
List of articles
The question
An array of positive integers beans, Everyone has beans[i] Bean . Take out some beans from each , Make not 0 The values of bits are the same , Find the minimum number of beans .
Examples
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 .
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 .
Ideas
We can beans After sorting from small to large , Enumerate the number of all final non empty beans x, Less than x Empty beans , Greater than x The number of beans decreased to x.
- result :0000xxxxx, What needs to be removed is :ans = sum - (len - i) * x;
Algorithm
Code
class Solution {
public:
long long minimumRemoval(vector<int>& beans) {
int len = beans.size();
long long sum = 0, res;
for (auto &x: beans) sum += x;
res = sum;
sort(beans.begin(), beans.end());
for (int i = 0; i < len; i++) {
res = min(res, sum - (len - i) * (long long)beans[i]);
}
return res;
}
};
边栏推荐
- PR 2021 quick start tutorial, material import and management
- Titles can only be retrieved in PHP via curl - header only retrieval in PHP via curl
- Global and Chinese market of electrolyte analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
- 2.1 use of variables
- 2.2 integer
- 2. Template syntax
- Chapter 1: simplify the same code decimal sum s (D, n)
- 2.3 other data types
- What is the difference between a kill process and a close process- What are the differences between kill process and close process?
- WPF format datetime in TextBlock- WPF format DateTime in TextBlock?
猜你喜欢

2.2 integer

BOC protected tryptophan zinc porphyrin (Zn · TAPP Trp BOC) / copper porphyrin (Cu · TAPP Trp BOC) / cobalt porphyrin (cobalt · TAPP Trp BOC) / iron porphyrin (Fe · TAPP Trp BOC) / Qiyue supply

Ae/pr/fcpx super visual effects plug-in package fxfactory

Sparse matrix (triple) creation, transpose, traversal, addition, subtraction, multiplication. C implementation

1.5 learn to find mistakes first

BOC protected alanine porphyrin compound TAPP ala BOC BOC BOC protected phenylalanine porphyrin compound TAPP Phe BOC Qi Yue supply

How to improve data security by renting servers in Hong Kong

NFT without IPFs and completely on the chain?

Phpstudy set LAN access

02 -- QT OpenGL drawing triangle
随机推荐
Chapter 1: find all factorial sums, Grand Prix site unified programming, three factorial sums, graphic point scanning, recursive factorial n of n!, Find the factorial n of n!, King Shehan miscalculate
2.3 other data types
Global and Chinese market of speed limiter 2022-2028: Research Report on technology, participants, trends, market size and share
Microsoft: the 12th generation core processor needs to be upgraded to win11 to give full play to its maximum performance
Chapter 2: find the number of daffodils based on decomposition, find the number of daffodils based on combination, find the conformal number in [x, y], explore the n-bit conformal number, recursively
unittest框架基本使用
2022-07-02 advanced network engineering (XV) routing policy - route policy feature, policy based routing, MQC (modular QoS command line)
Geek Daily: the system of monitoring employees' turnover intention has been deeply convinced off the shelves; The meta universe app of wechat and QQ was actively removed from the shelves; IntelliJ pla
Pat grade B 1009 is ironic (20 points)
Class loading process
BOC protected alanine porphyrin compound TAPP ala BOC BOC BOC protected phenylalanine porphyrin compound TAPP Phe BOC Qi Yue supply
Micro service knowledge sorting - cache technology
Part 27 supplement (27) buttons of QML basic elements
2.4 conversion of different data types
JMeter connection database
Day11 - my page, user information acquisition, modification and channel interface
2. Template syntax
Use unique_ PTR forward declaration? [repetition] - forward declaration with unique_ ptr? [duplicate]
kubernetes集群搭建efk日志收集平台
MPLS configuration