【LeetCode】Python | 1760. The minimum number of balls in the bag (two points)

foreword

        LeetCode walks back, happy like a fairy~


【LeetCode】Python | 1760. The minimum number of balls in the bag (two points)

topic

describe

Example 1:

Example 2:

Example 3:

hint:

 answer

ideas

Python code


topic

describe


        gives you an array of integers nums: 

        where nums[i] represents the number of balls in the ith bag.

        Also gives you an integer maxOperations . You can perform the following operations up to maxOperations times:

        Choose any bag and divide the balls in the bag into 2 new bags, each bag containing a positive integer number of balls. Let's say a bag has 5 balls, you can divide them into two new bags with 1 and 4 balls, or 2 and 3 balls.

Your overhead is the maximum          number of balls in a single bag, and you want to minimize  overhead. Please return the minimum overhead after doing the above operations.


Example 1:

Input : nums = [9], maxOperations = 2
Output : 3
Explanation:
- Divide bag with 9 balls into bags with 6 and 3 balls. [9] -> [6,3] .
- Divide the bag with 6 balls into bags with 3 and 3 balls. [6,3] -> [3,3,3] .
The bag with the most balls has 3 balls in it, so the cost is 3 and 3 is returned.

Example 2:

Input : nums = [2,4,8,2], maxOperations = 4
Output : 2
Explanation:
- Divide bag with 8 balls into bags with 4 and 4 balls. [2,4,8,2] -> [2,4,4,4,2] .
- Divide the bag with 4 balls into bags with 2 and 2 balls. [2,4,4,4,2] -> [2,2,2,4,4,2] .
- Divide the bag with 4 balls into bags with 2 and 2 balls. [2,2,2,4,4,2] -> [2,2,2,2,2,4,2] .
- Divide the bag with 4 balls into bags with 2 and 2 balls. [2,2,2,2,2,4,2] -> [2,2,2,2,2,2,2,2] .
The bag with the most balls contains 2 balls, so the cost is 2 and 2 is returned.

Example 3:

Input: nums = [7,17], maxOperations = 2
Output: 7


hint:

  • 1 <= nums.length <= 105
  • 1 <= maxOperations, nums[i] <= 109

 answer

ideas


         First of all, from the title, we can sensitively delineate the words such as maximum minimization. Obviously, this is the characteristic of the binary algorithm, so we first infer that this is a binary problem. Of course, we can't be so simple and arbitrary. Further analysis is required.

        According to the meaning of the title,

        We need to divide the balls in one bag into two new bags, and we can perform this operation max_operation times.

        So we can get :

       In order to make the overhead as small as possible, we define such a number N: while dividing each bag into N small balls, the sum of the times of division of each bag <= the maximum number of times (max_operation), then I call it like this A number is divisible.

        for example:

        Set N=8, max_operation=3, nums=[16,17], then, 16 is divided into  8,8 , which requires 16//8-1=2-1=1 times, and 17 is divided into  8,8,1 , Need to divide 17//8=2 times.

        At this point, N=8 is a divisible number,

        In addition, we can easily summarize the law: when a number M%N==0, the number of divisions operation_iter=M//N-1 times, and vice versa, the number of divisions operation_iter=M//N times.

        So can this question be transformed into:

        How to find a smallest divisible number?

        Therefore, we use the bisection setting l=1, r=int(1e9), and the initial number N is l+r>>1: 

        When N satisfies the condition, reduce this number to see if a smaller overhead can be found;

        When N does not satisfy the condition, we increase this number to see if we can find an overhead that satisfies the condition.

        In this way, it really becomes a binary search problem!

        Case Analysis:

        In example 3, assuming that our current binary search calculates a maximum cost of 7, then during the verification process, we traverse the entire array and expect to divide all the balls into bags, each of which does not exceed 7. Then the first element 7 naturally does not need to be decomposed, and the second element 17, 17 should be decomposed into three bags of 7 + 7 + 3 (only 2 operations are required). After traversing the entire array, only 2 operations are required in total, which does not exceed the maximum number of operations. Indicates that the currently calculated cost can be satisfied. Since the problem requires us to calculate the minimum cost, we need to continue to narrow the search range, hoping to find a smaller cost that meets the conditions.

Python code


class Solution:
    def check(self, nums, minValue, maxOperations):
        opt_iter = 0
        for x in nums:
            if x % minValue == 0:
                opt_iter += (x // minValue) - 1
            else:
                opt_iter += (x // minValue)
        return opt_iter <= maxOperations

    def minimumSize(self, nums: List[int], maxOperations: int) -> int:
        l = 1
        r = int(1e9)
        while l <= r:
            mid = l + r >> 1
            if self.check(nums, mid, maxOperations):
                r = mid - 1
            else:
                l = mid + 1
        return l

Guess you like

Origin blog.csdn.net/qq_51831335/article/details/127456763