|
| 1 | +/** |
| 2 | + * [1760] Minimum Limit of Balls in a Bag |
| 3 | + * |
| 4 | + * You are given an integer array nums where the i^th bag contains nums[i] balls. You are also given an integer maxOperations. |
| 5 | + * You can perform the following operation at most maxOperations times: |
| 6 | + * |
| 7 | + * Take any bag of balls and divide it into two new bags with a positive number of balls. |
| 8 | + * |
| 9 | + * For example, a bag of 5 balls can become two new bags of 1 and 4 balls, or two new bags of 2 and 3 balls. |
| 10 | + * |
| 11 | + * |
| 12 | + * |
| 13 | + * Your penalty is the maximum number of balls in a bag. You want to minimize your penalty after the operations. |
| 14 | + * Return the minimum possible penalty after performing the operations. |
| 15 | + * |
| 16 | + * Example 1: |
| 17 | + * |
| 18 | + * Input: nums = [9], maxOperatios = 2 |
| 19 | + * Output: 3 |
| 20 | + * Explanation: |
| 21 | + * - Divide the bag with 9 balls into two bags of sizes 6 and 3. [<u>9</u>] -> [6,3]. |
| 22 | + * - Divide the bag with 6 balls into two bags of sizes 3 and 3. [<u>6</u>,3] -> [3,3,3]. |
| 23 | + * The bag with the most number of balls has 3 balls, so your penalty is 3 and you should return 3. |
| 24 | + * |
| 25 | + * Example 2: |
| 26 | + * |
| 27 | + * Input: nums = [2,4,8,2], maxOperations = 4 |
| 28 | + * Output: 2 |
| 29 | + * Explanation: |
| 30 | + * - Divide the bag with 8 balls into two bags of sizes 4 and 4. [2,4,<u>8</u>,2] -> [2,4,4,4,2]. |
| 31 | + * - Divide the bag with 4 balls into two bags of sizes 2 and 2. [2,<u>4</u>,4,4,2] -> [2,2,2,4,4,2]. |
| 32 | + * - Divide the bag with 4 balls into two bags of sizes 2 and 2. [2,2,2,<u>4</u>,4,2] -> [2,2,2,2,2,4,2]. |
| 33 | + * - Divide the bag with 4 balls into two bags of sizes 2 and 2. [2,2,2,2,2,<u>4</u>,2] -> [2,2,2,2,2,2,2,2]. |
| 34 | + * The bag with the most number of balls has 2 balls, so your penalty is 2, and you should return 2. |
| 35 | + * |
| 36 | + * |
| 37 | + * Constraints: |
| 38 | + * |
| 39 | + * 1 <= nums.length <= 10^5 |
| 40 | + * 1 <= maxOperations, nums[i] <= 10^9 |
| 41 | + * |
| 42 | + */ |
| 43 | +pub struct Solution {} |
| 44 | + |
| 45 | +// problem: https://leetcode.com/problems/minimum-limit-of-balls-in-a-bag/ |
| 46 | +// discuss: https://leetcode.com/problems/minimum-limit-of-balls-in-a-bag/discuss/?currentPage=1&orderBy=most_votes&query= |
| 47 | + |
| 48 | +// submission codes start here |
| 49 | + |
| 50 | +impl Solution { |
| 51 | + pub fn minimum_size(nums: Vec<i32>, max_operations: i32) -> i32 { |
| 52 | + let (mut min, mut max) = (1, *nums.iter().max().unwrap()); |
| 53 | + |
| 54 | + let mut penalty; |
| 55 | + |
| 56 | + while min < max { |
| 57 | + penalty = (min + max) / 2; |
| 58 | + if Self::can_fit_operations(penalty, &nums, max_operations) { |
| 59 | + max = penalty; |
| 60 | + } else { |
| 61 | + min = penalty + 1; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + min |
| 66 | + } |
| 67 | + fn can_fit_operations(penalty: i32, nums: &Vec<i32>, max: i32) -> bool { |
| 68 | + let mut result = 0; |
| 69 | + |
| 70 | + for num in nums { |
| 71 | + result += (num - 1) / penalty; |
| 72 | + if result > max { |
| 73 | + return false; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + true |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +// submission codes end |
| 82 | + |
| 83 | +#[cfg(test)] |
| 84 | +mod tests { |
| 85 | + use super::*; |
| 86 | + |
| 87 | + #[test] |
| 88 | + fn test_1760_example_1() { |
| 89 | + let nums = vec![9]; |
| 90 | + let max_operations = 2; |
| 91 | + |
| 92 | + let result = 3; |
| 93 | + |
| 94 | + assert_eq!(Solution::minimum_size(nums, max_operations), result); |
| 95 | + } |
| 96 | + |
| 97 | + #[test] |
| 98 | + fn test_1760_example_2() { |
| 99 | + let nums = vec![2, 4, 8, 2]; |
| 100 | + let max_operations = 4; |
| 101 | + |
| 102 | + let result = 2; |
| 103 | + |
| 104 | + assert_eq!(Solution::minimum_size(nums, max_operations), result); |
| 105 | + } |
| 106 | +} |
0 commit comments