当前位置:网站首页>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;
}
};
边栏推荐
- unittest框架基本使用
- [effective Objective-C] - block and grand central distribution
- WPF format datetime in TextBlock- WPF format DateTime in TextBlock?
- 2022 - 06 - 30 networker Advanced (XIV) Routing Policy Matching Tool [ACL, IP prefix list] and policy tool [Filter Policy]
- Day11 - my page, user information acquisition, modification and channel interface
- Wechat applet quick start (including NPM package use and mobx status management)
- BOC protected tryptophan porphyrin compound (TAPP Trp BOC) Pink Solid 162.8mg supply - Qiyue supply
- HCIA-USG Security Policy
- Cross compile opencv with contrib
- Nacos usage of micro services
猜你喜欢

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

Chapter 1: find the factorial n of n!

FAQs for datawhale learning!

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

2022-06-25 网工进阶(十一)IS-IS-三大表(邻居表、路由表、链路状态数据库表)、LSP、CSNP、PSNP、LSP的同步过程

BOC protected alanine zinc porphyrin Zn · TAPP ala BOC / alanine zinc porphyrin Zn · TAPP ala BOC / alanine zinc porphyrin Zn · TAPP ala BOC / alanine zinc porphyrin Zn · TAPP ala BOC supplied by Qiyu

Professional interpretation | how to become an SQL developer

2022-06-27 网工进阶(十二)IS-IS-开销类型、开销计算、LSP的处理机制、路由撤销、路由渗透
![[Yu Yue education] basic reference materials of manufacturing technology of Shanghai Jiaotong University](/img/95/5baf5c8bedb00e67394a6c0a8234ff.png)
[Yu Yue education] basic reference materials of manufacturing technology of Shanghai Jiaotong University

The 15 year old interviewer will teach you four unique skills that you must pass the interview
随机推荐
Professional interpretation | how to become an SQL developer
Global and Chinese market of micro positioning technology 2022-2028: Research Report on technology, participants, trends, market size and share
2. Template syntax
FAQs for datawhale learning!
Rd file name conflict when extending a S4 method of some other package
Day10 -- forced login, token refresh and JWT disable
PR 2021 quick start tutorial, material import and management
第二章:4位卡普雷卡数,搜索偶数位卡普雷卡数,搜索n位2段和平方数,m位不含0的巧妙平方数,指定数字组成没有重复数字的7位平方数,求指定区间内的勾股数组,求指定区间内的倒立勾股数组
Native table - scroll - merge function
03 -- QT OpenGL EBO draw triangle
Exercises of function recursion
BOC protected alanine porphyrin compound TAPP ala BOC BOC BOC protected phenylalanine porphyrin compound TAPP Phe BOC Qi Yue supply
2022-06-30 網工進階(十四)路由策略-匹配工具【ACL、IP-Prefix List】、策略工具【Filter-Policy】
Chapter 2: find the classical solution of the maximum Convention and the least common multiple of a and B, find the conventional solution of the maximum Convention and the least common multiple of a a
2.4 conversion of different data types
2022-07-02 advanced network engineering (XV) routing policy - route policy feature, policy based routing, MQC (modular QoS command line)
Chapter 1: seek common? Decimal and S (D, n)
Part 28 supplement (XXVIII) busyindicator (waiting for elements)
NFT without IPFs and completely on the chain?
Xctf attack and defense world crypto master advanced area olddriver