You are given an array of positive integers beans, where each integer represents the number of magic beans found in a particular magic bag.
Remove any number of beans (possibly none) from each bag such that the number of beans in each remaining non-empty bag (still containing at least one bean) is equal. Once a bean has been removed from a bag, you are not allowed to return it to any of the bags.
Return theminimum number of magic beans that you have to remove.
Input: beans =[4,1,6,5] Output:4 Explanation:- We remove 1 bean from the bag with only 1 bean. This results in the remaining bags:[4,**_0_**,6,5]- Then we remove 2 beans from the bag with6 beans. This results in the remaining bags:[4,0,**_4_**,5]- Then we remove 1 bean from the bag with5 beans. This results in the remaining bags:[4,0,4,**_4_**] We removed a total of 1+2+1=4 beans to make the remaining non-empty bags have an equal number of beans. There are no other solutions that remove 4 beans or fewer.
Input: beans =[2,10,3,2] Output:7 Explanation:- We remove 2 beans from one of the bags with2 beans. This results in the remaining bags:[_**0**_ ,10,3,2]- Then we remove 2 beans from the other bag with2 beans. This results in the remaining bags:[0,10,3,_**0**_]- Then we remove 3 beans from the bag with3 beans. This results in the remaining bags:[0,10,_**0**_ ,0] We removed a total of 2+2+3=7 beans to make the remaining non-empty bags have an equal number of beans. There are no other solutions that removes 7 beans or fewer.
To minimize the number of beans removed, we want to keep all bags with at least x beans (for some x) and remove all beans from bags with less than x. For each possible x (each unique value in beans), calculate the cost to make all non-empty bags have x beans.
#include<vector>#include<algorithm>#include<numeric>usingnamespace std;
longlongminimumRemoval(vector<int>& beans) {
sort(beans.begin(), beans.end());
longlong total = accumulate(beans.begin(), beans.end(), 0LL);
longlong res = total;
int n = beans.size();
for (int i =0; i < n; ++i) {
res = min(res, total -1LL* beans[i] * (n - i));
}
return res;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import"sort"funcminimumRemoval(beans []int) int64 {
sort.Ints(beans)
vartotalint64for_, v:=rangebeans {
total+= int64(v)
}
res:=totaln:= len(beans)
fori, v:=rangebeans {
rem:=total- int64(v)*(int64(n-i))
ifrem < res {
res = rem }
}
returnres}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.*;
publicclassSolution {
publiclongminimumRemoval(int[] beans) {
Arrays.sort(beans);
long total = 0;
for (int b : beans) total += b;
long res = total;
int n = beans.length;
for (int i = 0; i < n; i++) {
res = Math.min(res, total - (long)beans[i]* (n - i));
}
return res;
}
}
1
2
3
4
5
6
7
8
9
10
funminimumRemoval(beans: IntArray): Long {
beans.sort()
val total = beans.map { it.toLong() }.sum()
var res = total
val n = beans.size
for (i in beans.indices) {
res = minOf(res, total - beans[i].toLong() * (n - i))
}
return res
}
1
2
3
4
5
defminimumRemoval(beans):
beans.sort()
total = sum(beans)
n = len(beans)
return min(total - beans[i] * (n - i) for i in range(n))
1
2
3
4
5
6
7
8
9
10
11
12
13
fnminimum_removal(mut beans: Vec<i64>) -> i64 {
beans.sort();
let total: i64= beans.iter().sum();
let n = beans.len() asi64;
letmut res = total;
for (i, &b) in beans.iter().enumerate() {
let rem = total - b * (n - i asi64);
if rem < res {
res = rem;
}
}
res
}
1
2
3
4
5
6
7
8
9
10
functionminimumRemoval(beans: number[]):number {
beans.sort((a, b) =>a-b);
consttotal=beans.reduce((a, b) =>a+b, 0);
constn=beans.length;
letres=total;
for (leti=0; i<n; i++) {
res= Math.min(res, total-beans[i] * (n-i));
}
returnres;
}