返回介绍

solution / 2200-2299 / 2226.Maximum Candies Allocated to K Children / README_EN

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

2226. Maximum Candies Allocated to K Children

中文文档

Description

You are given a 0-indexed integer array candies. Each element in the array denotes a pile of candies of size candies[i]. You can divide each pile into any number of sub piles, but you cannot merge two piles together.

You are also given an integer k. You should allocate piles of candies to k children such that each child gets the same number of candies. Each child can take at most one pile of candies and some piles of candies may go unused.

Return _the maximum number of candies each child can get._

 

Example 1:

Input: candies = [5,8,6], k = 3
Output: 5
Explanation: We can divide candies[1] into 2 piles of size 5 and 3, and candies[2] into 2 piles of size 5 and 1. We now have five piles of candies of sizes 5, 5, 3, 5, and 1. We can allocate the 3 piles of size 5 to 3 children. It can be proven that each child cannot receive more than 5 candies.

Example 2:

Input: candies = [2,5], k = 11
Output: 0
Explanation: There are 11 children but only 7 candies in total, so it is impossible to ensure each child receives at least one candy. Thus, each child gets no candy and the answer is 0.

 

Constraints:

  • 1 <= candies.length <= 105
  • 1 <= candies[i] <= 107
  • 1 <= k <= 1012

Solutions

Solution 1

class Solution:
  def maximumCandies(self, candies: List[int], k: int) -> int:
    left, right = 0, max(candies)
    while left < right:
      mid = (left + right + 1) >> 1
      cnt = sum(v // mid for v in candies)
      if cnt >= k:
        left = mid
      else:
        right = mid - 1
    return left
class Solution {
  public int maximumCandies(int[] candies, long k) {
    int left = 0, right = (int) 1e7;
    while (left < right) {
      int mid = (left + right + 1) >> 1;
      long cnt = 0;
      for (int v : candies) {
        cnt += v / mid;
      }
      if (cnt >= k) {
        left = mid;
      } else {
        right = mid - 1;
      }
    }
    return left;
  }
}
class Solution {
public:
  int maximumCandies(vector<int>& candies, long long k) {
    int left = 0, right = 1e7;
    while (left < right) {
      int mid = (left + right + 1) >> 1;
      long long cnt = 0;
      for (int& v : candies) cnt += v / mid;
      if (cnt >= k)
        left = mid;
      else
        right = mid - 1;
    }
    return left;
  }
};
func maximumCandies(candies []int, k int64) int {
  left, right := 0, int(1e7)
  for left < right {
    mid := (left + right + 1) >> 1
    var cnt int64
    for _, v := range candies {
      cnt += int64(v / mid)
    }
    if cnt >= k {
      left = mid
    } else {
      right = mid - 1
    }
  }
  return left
}

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

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

发布评论

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