The Indian Engineer

Problem 1760 Minimum Limit of Balls in a Bag

Posted on 6 mins

Array Binary-Search

Problem Statement

Link - Problem 1760

Question

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.

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:

Solution

class Solution {
public:
    int minimumSize(vector<int>& nums, int max_operations) {
        int left = 1;
        int right = 0;

        for (auto num : nums) {
            right = max(right, num);
        }

        while (left < right) {
            int middle = (left + right) / 2;

            if (isPossible(middle, nums, max_operations)) {
                right = middle;
            }
            else {
                left = middle + 1;
            }
        }

        return left;
    }

private:
    bool isPossible(int maxBallsInBag, vector<int>& nums, int max_operations) {
        int total_operations = 0;

        for (int num : nums) {
            int operations = ceil(num / (double)maxBallsInBag) - 1;
            total_operations += operations;

            if (total_operations > max_operations)
                return false;
        }

        return true;
    }
};

Complexity Analysis

| Algorithm     | Time Complexity | Space Complexity |
| ------------- | --------------- | ---------------- |
| Binary Search | O(n log m)      | O(1)             |

Explanation

Intial Thoughts

The goal is to minimize the maximum number of balls in a bag. Given an operation to divide a bag into two with a positive number of balls, think about the strategy for dividing the bags. The operation seems to reduce the maximum number of balls by repeatedly dividing the largest bag. Consider using a search strategy, like binary search, to efficiently find the smallest possible maximum. Identify the key factors that influence the minimum penalty, including the range of possible maximum balls and the total number of operations allowed.

Intuitive Analysis

First, determine the feasible range for the minimum penalty, from the smallest possible maximum (1 ball in a bag) to the largest possible maximum (the size of the largest bag without any operations). Next, design a search space to explore this range and identify a suitable criterion to decide on the feasibility of a given maximum. This criterion is whether the total number of operations required to achieve or improve upon the given maximum is within the allowed limit. Perform a binary search to efficiently narrow down the search space and converge on the optimal solution.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Medium difficulty.

Hints

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


Similar Questions:

Title URL Difficulty
Maximum Candies Allocated to K Children https://leetcode.com/problems/maximum-candies-allocated-to-k-children Medium
Minimized Maximum of Products Distributed to Any Store https://leetcode.com/problems/minimized-maximum-of-products-distributed-to-any-store Medium