8000 Time: 32 ms (39.82%), Space: 88.3 MB (41.19%) - LeetHub · karygauss03/Leetcode-Solutions@9b3da19 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9b3da19

Browse files
committed
Time: 32 ms (39.82%), Space: 88.3 MB (41.19%) - LeetHub
1 parent cbcc666 commit 9b3da19

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
public:
3+
int maximumCandies(vector<int>& candies, long long k) {
4+
int maxCandiesInPile = 0;
5+
for (int i = 0; i < candies.size(); i++) {
6+
maxCandiesInPile = max(maxCandiesInPile, candies[i]);
7+
}
8+
9+
int left = 0;
10+
int right = maxCandiesInPile;
11+
12+
while (left < right) {
13+
int middle = (left + right + 1) / 2;
14+
if (canAllocateCandies(candies, k, middle)) {
15+
left = middle;
16+
} else {
17+
right = middle - 1;
18+
}
19+
}
20+
21+
return left;
22+
}
23+
24+
private:
25+
bool canAllocateCandies(vector<int>& candies, long long k,
26+
int numOfCandies) {
27+
long long int maxNumOfChildren = 0;
28+
29+
for (int pileIndex = 0; pileIndex < candies.size(); pileIndex++) {
30+
maxNumOfChildren +=
31+
candies[pileIndex] /
32+
numOfCandies;
33+
}
34+
35+
return maxNumOfChildren >= k;
36+
}
37+
};

0 commit comments

Comments
 (0)
0