1886 Minimum Limit Of Balls In A Bag
1886 Minimum Limit Of Balls In A Bag
Minimum Limit of Balls in a Bag 
You are given an integer array nums where the ith bag contains nums[i] balls. You are also given an integer maxOperations.
You can perform the following operation at most maxOperations times:
1
2
3
Take any bag of balls and divide it into two new bags with a **positive **number of balls.
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.
Your penalty is the maximum number of balls in a bag. You want to minimize your penalty after the operations.
Return the minimum possible penalty after performing the operations.
Example 1:
1
2
3
4
5
6
7
8
**Input:** nums = [9], maxOperations = 2
**Output:** 3
**Explanation:**
- Divide the bag with 9 balls into two bags of sizes 6 and 3. [**9**] -> [6,3].
- Divide the bag with 6 balls into two bags of sizes 3 and 3. [**6**,3] -> [3,3,3].
The bag with the most number of balls has 3 balls, so your penalty is 3 and you should return 3.
Example 2:
1
2
3
4
5
6
7
8
9
10
**Input:** nums = [2,4,8,2], maxOperations = 4
**Output:** 2
**Explanation:**
- Divide the bag with 8 balls into two bags of sizes 4 and 4. [2,4,**8**,2] -> [2,4,4,4,2].
- Divide the bag with 4 balls into two bags of sizes 2 and 2. [2,**4**,4,4,2] -> [2,2,2,4,4,2].
- Divide the bag with 4 balls into two bags of sizes 2 and 2. [2,2,2,**4**,4,2] -> [2,2,2,2,2,4,2].
- Divide the bag with 4 balls into two bags of sizes 2 and 2. [2,2,2,2,2,**4**,2] -> [2,2,2,2,2,2,2,2].
The bag with the most number of balls has 2 balls, so your penalty is 2, and you should return 2.
Constraints:
1
2
1 <= nums.length <= 105
1 <= maxOperations, nums[i] <= 109
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Solution:
def minimumSize(self, nums, max_operations):
# Binary search bounds
left = 1
right = max(nums)
# Perform binary search to find the optimal max_balls_in_bag
while left < right:
middle = (left + right) // 2
# Check if a valid distribution is possible with the current middle value
if self._is_possible(middle, nums, max_operations):
# If possible, try a smaller value (shift right to middle)
right = middle
else:
# If not possible, try a larger value (shift left to middle + 1)
left = middle + 1
# Return the smallest possible value for max_balls_in_bag
return left
# Helper function to check if a distribution is possible for a given max_balls_in_bag
def _is_possible(self, max_balls_in_bag, nums, max_operations):
total_operations = 0
# Iterate through each bag in the array
for num in nums:
# Calculate the number of operations needed to split this bag
operations = math.ceil(num / max_balls_in_bag) - 1
total_operations += operations
# If total operations exceed max_operations, return False
if total_operations > max_operations:
return False
# We can split the balls within the allowed operations, return True
return True
This post is licensed under CC BY 4.0 by the author.