当前位置:网站首页>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 <= 105
1 <= 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).
边栏推荐
- [EF core] mapping relationship between EF core and C data type
- 【luogu CF487E】Tourists(圆方树)(树链剖分)(线段树)
- 【在线聊天】原来微信小程序也能回复Facebook主页消息!
- FFMPEG关键结构体——AVFormatContext
- Configuring OSPF GR features for Huawei devices
- Online yaml to CSV tool
- Detailed explanation of APP functions of door-to-door appointment service
- 18. (ArcGIS API for JS) ArcGIS API for JS point collection (sketchviewmodel)
- QT--线程
- 剖面测量之提取剖面数据
猜你喜欢
Upgrade openssl-1.1.1p for openssl-1.0.2k
MySQL functions
亲测可用fiddler手机抓包配置代理后没有网络
Configuring OSPF GR features for Huawei devices
[day39 literature extensive reading] a Bayesian perspective on magnetic estimation
Huawei equipment configuration ospf-bgp linkage
权限问题:source .bash_profile permission denied
MySQL之函数
【NOI模拟赛】Anaid 的树(莫比乌斯反演,指数型生成函数,埃氏筛,虚树)
Huawei equipment is configured with OSPF and BFD linkage
随机推荐
wx.getLocation(Object object)申请方法,最新版
JS can really prohibit constant modification this time!
7.5 simulation summary
[day39 literature extensive reading] a Bayesian perspective on magnetic estimation
云呐|固定资产管理系统功能包括哪些?
MySQL之函数
shardingsphere源码解析
MySQL global lock and table lock
【luogu CF487E】Tourists(圆方树)(树链剖分)(线段树)
选择致敬持续奋斗背后的精神——对话威尔价值观【第四期】
微信小程序---WXML 模板语法(附带笔记文档)
Problems encountered in the database
用列表初始化你的vector&&initializer_list简介
【DesignMode】适配器模式(adapter pattern)
18.(arcgis api for js篇)arcgis api for js点采集(SketchViewModel)
N1 # if you work on a metauniverse product [metauniverse · interdisciplinary] Season 2 S2
Problem solving win10 quickly open ipynb file
XML配置文件(DTD详细讲解)
第16章 OAuth2AuthorizationRequestRedirectWebFilter源码解析
JS 这次真的可以禁止常量修改了!