1760. Minimum Limit of Balls in a Bag Medium

@problem@discussion
#Array#Binary Search



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], maxOperations = 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 an you should return 2.
35 * 
36 * Example 3:
37 * 
38 * Input: nums = [7,17], maxOperations = 2
39 * Output: 7
40 * 
41 *  
42 * Constraints:
43 * 
44 * 	1 <= nums.length <= 10^5
45 * 	1 <= maxOperations, nums[i] <= 10^9
46 * 
47 */
48pub struct Solution {}
49
50// problem: https://leetcode.com/problems/minimum-limit-of-balls-in-a-bag/
51// discuss: https://leetcode.com/problems/minimum-limit-of-balls-in-a-bag/discuss/?currentPage=1&orderBy=most_votes&query=
52
53// submission codes start here
54
55impl Solution {
56    pub fn minimum_size(nums: Vec<i32>, max_operations: i32) -> i32 {
57        0
58    }
59}
60
61// submission codes end
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66
67    #[test]
68    fn test_1760() {
69    }
70}
71


Back
© 2025 bowen.ge All Rights Reserved.