当前位置:网站首页>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;
}
};
边栏推荐
- Cesiumjs 2022 ^ source code interpretation [7] - Analysis of the request and loading process of 3dfiles
- JMeter connection database
- PR FAQ: how to set PR vertical screen sequence?
- 2.7 format output of values
- MPLS configuration
- 44. Concurrent programming theory
- BOC protected phenylalanine zinc porphyrin (Zn · TAPP Phe BOC) / iron porphyrin (Fe · TAPP Phe BOC) / nickel porphyrin (Ni · TAPP Phe BOC) / manganese porphyrin (Mn · TAPP Phe BOC) Qiyue Keke
- Cross compile opencv with contrib
- Blue Bridge Cup: the fourth preliminary - "simulated intelligent irrigation system"
- Implementation of stack
猜你喜欢

Acquisition and transmission of parameters in automatic testing of JMeter interface

Today's work summary and plan: February 14, 2022

2022 Xinjiang latest construction eight members (standard members) simulated examination questions and answers

第一章:求奇因数代数和,求同吗小数和s(d, n),简化同码小数和s(d, n),拓广同码小数和s(d, n)

Chapitre 1: le roi de shehan a mal calculé

2.1 use of variables

05 -- QT OpenGL draw cube uniform

Nerfplusplus parameter format sorting

Chapter 1: find the algebraic sum of odd factors, find the same decimal sum s (D, n), simplify the same code decimal sum s (D, n), expand the same code decimal sum s (D, n)
![How to read the source code [debug and observe the source code]](/img/0d/6495c5da40ed1282803b25746a3f29.jpg)
How to read the source code [debug and observe the source code]
随机推荐
Detailed explanation of shuttle unity interworking principle
How to check the permission to write to a directory or file- How do you check for permissions to write to a directory or file?
2022 Xinjiang latest construction eight members (standard members) simulated examination questions and answers
10 smart contract developer tools that miss and lose
Win10 share you don't have permission
Chapter 1: find the factorial n of n!
Detailed and not wordy. Share the win10 tutorial of computer reinstallation system
Test panghu was teaching you how to use the technical code to flirt with girls online on Valentine's Day 520
7. Data broker presentation
6. Data agent object Defineproperty method
[effective Objective-C] - block and grand central distribution
Global and Chinese markets of polyimide tubes for electronics 2022-2028: Research Report on technology, participants, trends, market size and share
Network security Kali penetration learning how to get started with web penetration how to scan based on nmap
Micro service knowledge sorting - three pieces of micro Service Technology
kubernetes集群搭建efk日志收集平台
Utilisation de base du cadre unitest
Professional interpretation | how to become an SQL developer
Typora charges, WTF? Still need support
Global and Chinese market of speed limiter 2022-2028: Research Report on technology, participants, trends, market size and share
2.3 other data types