当前位置:网站首页>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;
}
};
边栏推荐
- Class loading process
- 第二章:4位卡普雷卡数,搜索偶数位卡普雷卡数,搜索n位2段和平方数,m位不含0的巧妙平方数,指定数字组成没有重复数字的7位平方数,求指定区间内的勾股数组,求指定区间内的倒立勾股数组
- Virtual machine installation deepin system
- Global and Chinese market of full authority digital engine control (FADEC) 2022-2028: Research Report on technology, participants, trends, market size and share
- Leetcode daily question solution: 540 A single element in an ordered array
- [raid] [simple DP] mine excavation
- 03 -- QT OpenGL EBO draw triangle
- Day11 - my page, user information acquisition, modification and channel interface
- Strict data sheet of new features of SQLite 3.37.0
- 3. Data binding
猜你喜欢

Kubernetes cluster builds efk log collection platform

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

NFT without IPFs and completely on the chain?

HCIA-USG Security Policy

Derivation of decision tree theory

Leetcode 1189. Maximum number of balloons (special character count)

Detailed explanation of shuttle unity interworking principle

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)

Wechat applet quick start (including NPM package use and mobx status management)

04 -- QT OpenGL two sets of shaders draw two triangles
随机推荐
Detailed and not wordy. Share the win10 tutorial of computer reinstallation system
Cross compile opencv with contrib
Network security Kali penetration learning how to get started with web penetration how to scan based on nmap
Global and Chinese markets of lithium chloride 2022-2028: Research Report on technology, participants, trends, market size and share
How to improve data security by renting servers in Hong Kong
Use unique_ PTR forward declaration? [repetition] - forward declaration with unique_ ptr? [duplicate]
Rd file name conflict when extending a S4 method of some other package
Chapter 1: drinking soft drinks, step tariff calculation, step tariff calculation function, personal income tax, solving square root inequality, simplifying solving square root inequality, solving dem
44. Concurrent programming theory
3. Data binding
Global and Chinese market of full authority digital engine control (FADEC) 2022-2028: Research Report on technology, participants, trends, market size and share
2022 - 06 - 30 networker Advanced (XIV) Routing Policy Matching Tool [ACL, IP prefix list] and policy tool [Filter Policy]
Micro service knowledge sorting - asynchronous communication technology
02 -- QT OpenGL drawing triangle
Strict data sheet of new features of SQLite 3.37.0
February 14-20, 2022 (osgear source code debugging +ue4 video +ogremain source code transcription)
PR notes:
Global and Chinese markets of polyimide tubes for electronics 2022-2028: Research Report on technology, participants, trends, market size and share
Xctf attack and defense world crypto master advanced area olddriver
Part 28 supplement (XXVIII) busyindicator (waiting for elements)