2022-03-27 20:38:29 +08:00
< p > You are given an array of < strong > positive< / strong > integers < code > beans< / code > , where each integer represents the number of magic beans found in a particular magic bag.< / p >
< p > < strong > Remove< / strong > any number of beans (< strong > possibly none< / strong > ) from each bag such that the number of beans in each remaining < strong > non-empty< / strong > bag (still containing < strong > at least one< / strong > bean) is < strong > equal< / strong > . Once a bean has been removed from a bag, you are < strong > not< / strong > allowed to return it to any of the bags.< / p >
< p > Return < em > the < strong > minimum< / strong > number of magic beans that you have to remove< / em > .< / p >
< p > < / p >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 1:< / strong > < / p >
2022-03-27 20:38:29 +08:00
< pre >
2023-12-09 18:42:21 +08:00
< strong > Input:< / strong > beans = [4,1,6,5]
2022-03-27 20:38:29 +08:00
< strong > Output:< / strong > 4
< strong > Explanation:< / strong >
- We remove 1 bean from the bag with only 1 bean.
2023-12-09 18:42:21 +08:00
This results in the remaining bags: [4,< strong > < u > 0< / u > < / strong > ,6,5]
2022-03-27 20:38:29 +08:00
- Then we remove 2 beans from the bag with 6 beans.
This results in the remaining bags: [4,0,< strong > < u > 4< / u > < / strong > ,5]
- Then we remove 1 bean from the bag with 5 beans.
2023-12-09 18:42:21 +08:00
This results in the remaining bags: [4,0,4,< strong > < u > 4< / u > < / strong > ]
2022-03-27 20:38:29 +08:00
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.
< / pre >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 2:< / strong > < / p >
2022-03-27 20:38:29 +08:00
< pre >
2023-12-09 18:42:21 +08:00
< strong > Input:< / strong > beans = [2,10,3,2]
2022-03-27 20:38:29 +08:00
< strong > Output:< / strong > 7
< strong > Explanation:< / strong >
- We remove 2 beans from one of the bags with 2 beans.
This results in the remaining bags: [< u > < strong > 0< / strong > < / u > ,10,3,2]
- Then we remove 2 beans from the other bag with 2 beans.
This results in the remaining bags: [0,10,3,< u > < strong > 0< / strong > < / u > ]
- Then we remove 3 beans from the bag with 3 beans.
This results in the remaining bags: [0,10,< u > < strong > 0< / strong > < / u > ,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.
< / pre >
< p > < / p >
< p > < strong > Constraints:< / strong > < / p >
< ul >
< li > < code > 1 < = beans.length < = 10< sup > 5< / sup > < / code > < / li >
< li > < code > 1 < = beans[i] < = 10< sup > 5< / sup > < / code > < / li >
< / ul >