LeetCode-1760. Minimum Limit of Balls in a Bag [Medium]-Analysis and Code (Java)

LeetCode-1760. Minimum Limit of Balls in a Bag [Minimum Limit of Balls in a Bag] [Medium]-Analysis and Code [Java]

1. Topic

Give you an integer array nums, where nums[i] represents the number of balls in the i-th bag. At the same time give 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 with a positive integer number of balls.
  • For example, if there are 5 balls in a bag, you can divide them into two new bags, 1 and 4 balls respectively, or 2 and 3 balls respectively.

Your cost is the maximum number of balls in a single bag, and you want to minimize the cost.
Please go back to the minimum cost after performing the above operations.

Example 1:

输入:nums = [9], maxOperations = 2
输出:3
解释:
- 将装有 9 个球的袋子分成装有 6 个和 3 个球的袋子。[9] -> [6,3] 。
- 将装有 6 个球的袋子分成装有 3 个和 3 个球的袋子。[6,3] -> [3,3,3] 。
装有最多球的袋子里装有 3 个球,所以开销为 3 并返回 3 。

Example 2:

输入:nums = [2,4,8,2], maxOperations = 4
输出:2
解释:
- 将装有 8 个球的袋子分成装有 4 个和 4 个球的袋子。[2,4,8,2] -> [2,4,4,4,2] 。
- 将装有 4 个球的袋子分成装有 2 个和 2 个球的袋子。[2,4,4,4,2] -> [2,2,2,4,4,2] 。
- 将装有 4 个球的袋子分成装有 2 个和 2 个球的袋子。[2,2,2,4,4,2] -> [2,2,2,2,2,4,2] 。
- 将装有 4 个球的袋子分成装有 2 个和 2 个球的袋子。[2,2,2,2,2,4,2] -> [2,2,2,2,2,2,2,2] 。
装有最多球的袋子里装有 2 个球,所以开销为 2 并返回 2 。

Example 3:

输入:nums = [7,17], maxOperations = 2
输出:7

prompt:

  • 1 <= nums.length <= 10^5
  • 1 <= maxOperations, nums[i] <= 10^9

Source: LeetCode (LeetCode)
Link: https://leetcode-cn.com/problems/minimum-limit-of-balls-in-a-bag The
copyright is owned by LeetCode . For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

Two, analysis and code

1. Dichotomy

(1) Thinking

For the balls of nums[i] in a single bag, the number of operations whose cost does not exceed spend is (nums[i]-1) / spend.
The dichotomy can be combined to find the minimum overhead under the condition that the maximum number of operations is met.

(2) Code

class Solution {
    
    
    public int minimumSize(int[] nums, int maxOperations) {
    
    
        int l = 1, r = 1000000000;
        while (l < r) {
    
    //二分法
            int m = (l + r) >> 1;
            if (check(nums, maxOperations, m))
                r = m;
            else
                l = m + 1;
        }
        return l;
    }

    public boolean check(int[] nums, int op, int spend) {
    
    
        for (int num : nums) {
    
    
            if (num > spend) {
    
    
                op -= (num - 1) / spend;//等效于当 num % spend == 0 时 -1
                if (op < 0)
                    return false;
            }
        }
        return true;
    }
}

(3) Results

Execution time: 37 ms, beating 89.67% of users
in all Java submissions ; memory consumption: 50.6 MB, beating 79.71% of users in all Java submissions.

Three, other

Nothing.

Guess you like

Origin blog.csdn.net/zml66666/article/details/113959834