2171. Removing Minimum Number of Magic Beans Medium
1/**
2 * [2171] Removing Minimum Number of Magic Beans
3 *
4 * You are given an array of positive integers beans, where each integer represents the number of magic beans found in a particular magic bag.
5 * 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.
6 * Return the minimum number of magic beans that you have to remove.
7 *
8 * Example 1:
9 *
10 * Input: beans = [4,<u>1</u>,6,5]
11 * Output: 4
12 * Explanation:
13 * - We remove 1 bean from the bag with only 1 bean.
14 * This results in the remaining bags: [4,<u>0</u>,6,5]
15 * - Then we remove 2 beans from the bag with 6 beans.
16 * This results in the remaining bags: [4,0,<u>4</u>,5]
17 * - Then we remove 1 bean from the bag with 5 beans.
18 * This results in the remaining bags: [4,0,4,<u>4</u>]
19 * We removed a total of 1 + 2 + 1 = 4 beans to make the remaining non-empty bags have an equal number of beans.
20 * There are no other solutions that remove 4 beans or fewer.
21 *
22 * Example 2:
23 *
24 * Input: beans = [<u>2</u>,10,<u>3</u>,<u>2</u>]
25 * Output: 7
26 * Explanation:
27 * - We remove 2 beans from one of the bags with 2 beans.
28 * This results in the remaining bags: [<u>0</u>,10,3,2]
29 * - Then we remove 2 beans from the other bag with 2 beans.
30 * This results in the remaining bags: [0,10,3,<u>0</u>]
31 * - Then we remove 3 beans from the bag with 3 beans.
32 * This results in the remaining bags: [0,10,<u>0</u>,0]
33 * We removed a total of 2 + 2 + 3 = 7 beans to make the remaining non-empty bags have an equal number of beans.
34 * There are no other solutions that removes 7 beans or fewer.
35 *
36 *
37 * Constraints:
38 *
39 * 1 <= beans.length <= 10^5
40 * 1 <= beans[i] <= 10^5
41 *
42 */
43pub struct Solution {}
44
45// problem: https://leetcode.com/problems/removing-minimum-number-of-magic-beans/
46// discuss: https://leetcode.com/problems/removing-minimum-number-of-magic-beans/discuss/?currentPage=1&orderBy=most_votes&query=
47
48// submission codes start here
49
50impl Solution {
51 pub fn minimum_removal(beans: Vec<i32>) -> i64 {
52
53 }
54}
55
56// submission codes end
57
58#[cfg(test)]
59mod tests {
60 use super::*;
61
62 #[test]
63 fn test_2171() {
64 }
65}
66
Back
© 2025 bowen.ge All Rights Reserved.