1760. Minimum Limit of Balls in a Bag
Difficulty: Medium
Topics: Array
, Binary Search
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:
- 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 of1
and4
balls, or two new bags of2
and3
balls.
- For example, a bag of
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:
- 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:
- 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 <= nums.length <= 105
1 <= maxOperations, nums[i] <= 109
Hint:
- Let's change the question if we know the maximum size of a bag what is the minimum number of bags you can make
- note that as the maximum size increases the minimum number of bags decreases so we can binary search the maximum size
Solution:
We can use binary search to find the minimum possible penalty. The key insight is that if we can determine whether a given penalty is achievable, we can narrow down the search range using binary search.
Steps to Solve:
-
Binary Search Setup:
- The minimum penalty is
1
(all balls are split into single-ball bags). - The maximum penalty is the largest number in the
nums
array.
- The minimum penalty is
-
Feasibility Check:
- For a given penalty
mid
, check if it's possible to achieve it with at mostmaxOperations
splits. - To do this, for each bag size in
nums
, calculate the number of splits required to make all bags havemid
balls or fewer. If the total splits exceedmaxOperations
, the penaltymid
is not feasible.
- For a given penalty
-
Iterate:
- Use binary search to adjust the range
[low, high]
based on whether a penaltymid
is feasible.
- Use binary search to adjust the range
Let's implement this solution in PHP: 1760. Minimum Limit of Balls in a Bag
<?php
/**
* @param Integer[] $nums
* @param Integer $maxOperations
* @return Integer
*/
function minimumSize($nums, $maxOperations) {
...
...
...
/**
* go to ./solution.php
*/
}
/**
* Helper function to check if a penalty is feasible
*
* @param $nums
* @param $maxOperations
* @param $penalty
* @return bool
*/
function canAchievePenalty($nums, $maxOperations, $penalty) {
...
...
...
/**
* go to ./solution.php
*/
}
// Example 1
$nums = [9];
$maxOperations = 2;
echo minimumSize($nums, $maxOperations); // Output: 3
// Example 2
$nums = [2, 4, 8, 2];
$maxOperations = 4;
echo minimumSize($nums, $maxOperations); // Output: 2
?>
Explanation:
-
Binary Search:
- The search space is between
1
and the maximum number in thenums
array. - The midpoint
mid
represents the current penalty we are testing.
- The search space is between
-
Feasibility Check (
canAchievePenalty
):- For each bag, calculate the required splits to ensure all bags have
mid
balls or fewer:-
ceil(balls / mid) - 1
gives the number of splits needed.
-
- If the total splits exceed
maxOperations
, the penalty is not feasible.
- For each bag, calculate the required splits to ensure all bags have
-
Adjust Search Space:
- If the penalty is feasible, reduce the upper bound (
high = mid
). - If not, increase the lower bound (
low = mid + 1
).
- If the penalty is feasible, reduce the upper bound (
-
Result:
- When the loop exits,
low
contains the smallest feasible penalty.
- When the loop exits,
Complexity:
-
Time Complexity: O(n . log(max(nums)))
- Binary search runs in O(log(max(nums))), and the feasibility check for each midpoint takes O(n).
- Space Complexity: O(1), as we only use constant extra space.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me:
Top comments (0)