Skip to content

Commit 612c55c

Browse files
committed
[LeetCode Sync] Runtime - 163 ms (91.04%), Memory - 29.6 MB (88.73%)
1 parent c53096f commit 612c55c

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<p>You are given a <strong>0-indexed</strong> integer array <code>candies</code>. Each element in the array denotes a pile of candies of size <code>candies[i]</code>. You can divide each pile into any number of <strong>sub piles</strong>, but you <strong>cannot</strong> merge two piles together.</p>
2+
3+
<p>You are also given an integer <code>k</code>. You should allocate piles of candies to <code>k</code> children such that each child gets the <strong>same</strong> number of candies. Each child can be allocated candies from <strong>only one</strong> pile of candies and some piles of candies may go unused.</p>
4+
5+
<p>Return <em>the <strong>maximum number of candies</strong> each child can get.</em></p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<pre>
11+
<strong>Input:</strong> candies = [5,8,6], k = 3
12+
<strong>Output:</strong> 5
13+
<strong>Explanation:</strong> 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.
14+
</pre>
15+
16+
<p><strong class="example">Example 2:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong> candies = [2,5], k = 11
20+
<strong>Output:</strong> 0
21+
<strong>Explanation:</strong> 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.
22+
</pre>
23+
24+
<p>&nbsp;</p>
25+
<p><strong>Constraints:</strong></p>
26+
27+
<ul>
28+
<li><code>1 &lt;= candies.length &lt;= 10<sup>5</sup></code></li>
29+
<li><code>1 &lt;= candies[i] &lt;= 10<sup>7</sup></code></li>
30+
<li><code>1 &lt;= k &lt;= 10<sup>12</sup></code></li>
31+
</ul>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Solution:
2+
def maximumCandies(self, candies: List[int], k: int) -> int:
3+
# get O(n) total number of candies
4+
total = sum(candies)
5+
6+
if total < k:
7+
# if total candies are less than the number of children, theres no equal division since some kid will be left hungry.
8+
return 0
9+
10+
# run a binary search between 1 and total/k, the ideal max size of candy pile for each kid
11+
l, r = 1, total // k
12+
13+
# we will store our final result as 0 initially
14+
res = 0
15+
16+
while l <= r:
17+
m = (l + r) // 2
18+
19+
# count will store number of piles for this iteration
20+
count = 0
21+
22+
# run a linear check to see if this result is valid or not
23+
# check for each candy in the array, if you can make piles of value [mid]
24+
for c in candies:
25+
count += c // m
26+
27+
# minor optimization, if anytime the count is above k, we can break the inner loop
28+
if count >= k:
29+
break
30+
31+
# check if count is valid or not (arithmetic check with k)
32+
if count >= k:
33+
# the result is valid
34+
res = m
35+
l = m+1
36+
else:
37+
# the result is not valid and search in lower half
38+
r = m-1
39+
40+
41+
return res

0 commit comments

Comments
 (0)