当前位置:网站首页>LeetCode 6006. Take out the least number of magic beans
LeetCode 6006. Take out the least number of magic beans
2022-07-06 00:09:00 【Daylight629】
6006. Take out the smallest number of magic beans
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 <= 1051 <= beans[i] <= 105
Two 、 Method 1
Sort + The prefix and
class Solution {
public long minimumRemoval(int[] beans) {
long res = Long.MAX_VALUE;
long sum = 0;
Arrays.sort(beans);
for (int i = 0; i < beans.length; i++) {
sum += beans[i];
}
for (int i = 0; i < beans.length; i++) {
long temp = 0;
temp += sum - (long)(beans.length - i) * beans[i];
res = Math.min(res, temp);
}
return res;
}
}
Complexity analysis
Time complexity :O(nlogn).
Spatial complexity :O(1).
边栏推荐
- Yunna | what are the main operating processes of the fixed assets management system
- [SQL] SQL expansion languages of mainstream databases (T-SQL, pl/sql, pl/pgsql)
- 上门预约服务类的App功能详解
- Open3D 点云随机添加噪声
- Zero rhino technology joined hands with the intelligence Club: the "causal faction" forum was successfully held, and the "causal revolution" brought the next generation of trusted AI
- 传输层协议------UDP协议
- GD32F4xx uIP协议栈移植记录
- 时区的区别及go语言的time库
- DEJA_ Vu3d - cesium feature set 055 - summary description of map service addresses of domestic and foreign manufacturers
- MySql——CRUD
猜你喜欢

软件测试工程师必会的银行存款业务,你了解多少?

微信小程序---WXML 模板语法(附带笔记文档)

Laser slam learning record

CAS and synchronized knowledge

总结了 800多个 Kubectl 别名,再也不怕记不住命令了!

Open source CRM customer relationship system management system source code, free sharing

Problems encountered in the database

Determinant learning notes (I)

Qt QPushButton详解

How much do you know about the bank deposit business that software test engineers must know?
随机推荐
Priority queue (heap)
mysql-全局锁和表锁
MySql——CRUD
Hudi of data Lake (2): Hudi compilation
【二叉搜索树】增删改查功能代码实现
Global and Chinese markets of POM plastic gears 2022-2028: Research Report on technology, participants, trends, market size and share
【luogu CF487E】Tourists(圆方树)(树链剖分)(线段树)
QT -- thread
wx.getLocation(Object object)申请方法,最新版
Problem solving win10 quickly open ipynb file
用列表初始化你的vector&&initializer_list简介
Redis high availability - master-slave replication, sentinel mode, cluster
MySQL之函数
数据库遇到的问题
15 MySQL stored procedures and functions
Global and Chinese markets of universal milling machines 2022-2028: Research Report on technology, participants, trends, market size and share
Which side projects can be achieved? Is it difficult for we media to earn more than 10000 a month?
Huawei equipment configuration ospf-bgp linkage
软件测试工程师必会的银行存款业务,你了解多少?
剖面测量之提取剖面数据