problemscpp
A collection of my answers to algorithm problems in c++.
静态 Public 成员函数 | 所有成员列表
leetcode::maximum_candies_allocated_to_k_children::Solution类 参考

#include <leetcode.h>

静态 Public 成员函数

static int maximumCandies (vector< int > &candies, long long k)
 

详细描述

在文件 leetcode.h2057 行定义.

成员函数说明

◆ maximumCandies()

int leetcode::maximum_candies_allocated_to_k_children::Solution::maximumCandies ( vector< int > &  candies,
long long  k 
)
static

在文件 leetcode.cpp5581 行定义.

5581 {
5582 sort(candies.begin(), candies.end());
5583 long long sum = 0;
5584 for(const auto candy: candies) {
5585 sum += candy;
5586 }
5587 int l = 1;
5588 int r = sum / k;
5589 if(sum < k) {
5590 return 0;
5591 }
5592 while(l < r) {
5593 if(l + 1 == r) {
5594 unsigned long long count = 0;
5595 for(auto it = candies.rbegin(); it != candies.rend() && *it >= r; ++it) {
5596 count += *it / r;
5597 if(count > k) {
5598 break;
5599 }
5600 }
5601 if(count > k) {
5602 return r;
5603 }
5604 return l;
5605 }
5606 const int m = (l + r) / 2;
5607 unsigned long long count = 0;
5608 for(auto it = candies.rbegin(); it != candies.rend() && *it >= m; ++it) {
5609 count += *it / m;
5610 if(count > k) {
5611 break;
5612 }
5613 }
5614 if(count >= k) {
5615 l = m;
5616 } else if(count < k) {
5617 r = m;
5618 }
5619 }
5620 return (l + r) / 2;
5621 }

被这些函数引用 leetcode::maximum_candies_allocated_to_k_children::TEST().


该类的文档由以下文件生成: