返回介绍

solution / 1700-1799 / 1760.Minimum Limit of Balls in a Bag / README_EN

发布于 2024-06-17 01:03:14 字数 5346 浏览 0 评论 0 收藏 0

1760. Minimum Limit of Balls in a Bag

中文文档

Description

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 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:

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

Solutions

Solution 1

class Solution:
  def minimumSize(self, nums: List[int], maxOperations: int) -> int:
    def check(mx: int) -> bool:
      return sum((x - 1) // mx for x in nums) <= maxOperations

    return bisect_left(range(1, max(nums)), True, key=check) + 1
class Solution {
  public int minimumSize(int[] nums, int maxOperations) {
    int left = 1, right = 0;
    for (int x : nums) {
      right = Math.max(right, x);
    }
    while (left < right) {
      int mid = (left + right) >> 1;
      long cnt = 0;
      for (int x : nums) {
        cnt += (x - 1) / mid;
      }
      if (cnt <= maxOperations) {
        right = mid;
      } else {
        left = mid + 1;
      }
    }
    return left;
  }
}
class Solution {
public:
  int minimumSize(vector<int>& nums, int maxOperations) {
    int left = 1, right = *max_element(nums.begin(), nums.end());
    while (left < right) {
      int mid = (left + right) >> 1;
      long long cnt = 0;
      for (int x : nums) {
        cnt += (x - 1) / mid;
      }
      if (cnt <= maxOperations) {
        right = mid;
      } else {
        left = mid + 1;
      }
    }
    return left;
  }
};
func minimumSize(nums []int, maxOperations int) int {
  r := slices.Max(nums)
  return 1 + sort.Search(r, func(mx int) bool {
    mx++
    cnt := 0
    for _, x := range nums {
      cnt += (x - 1) / mx
    }
    return cnt <= maxOperations
  })
}
function minimumSize(nums: number[], maxOperations: number): number {
  let left = 1;
  let right = Math.max(...nums);
  while (left < right) {
    const mid = (left + right) >> 1;
    let cnt = 0;
    for (const x of nums) {
      cnt += ~~((x - 1) / mid);
    }
    if (cnt <= maxOperations) {
      right = mid;
    } else {
      left = mid + 1;
    }
  }
  return left;
}
/**
 * @param {number[]} nums
 * @param {number} maxOperations
 * @return {number}
 */
var minimumSize = function (nums, maxOperations) {
  let left = 1;
  let right = Math.max(...nums);
  while (left < right) {
    const mid = (left + right) >> 1;
    let cnt = 0;
    for (const x of nums) {
      cnt += ~~((x - 1) / mid);
    }
    if (cnt <= maxOperations) {
      right = mid;
    } else {
      left = mid + 1;
    }
  }
  return left;
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文