Skip to content

Commit 6af72cf

Browse files
committed
Sync LeetCode submission Runtime - 367 ms (50.80%), Memory - 29.7 MB (88.94%)
1 parent cdebb3a commit 6af72cf

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Approach: Binary Search on The Answer
2+
3+
# n = len(candies), m = max(candies)
4+
# Time: O(n log m)
5+
# Space: O(1)
6+
7+
class Solution:
8+
def maximumCandies(self, candies: List[int], k: int) -> int:
9+
max_candies_in_pile = max(candies)
10+
11+
left = 0
12+
right = max_candies_in_pile
13+
14+
while left < right:
15+
mid = (left + right + 1) // 2
16+
17+
if self._can_allocate_candies(candies, k, mid):
18+
# If possible, move to the upper half to search for a larger number
19+
left = mid
20+
else:
21+
right = mid - 1
22+
23+
return left
24+
25+
def _can_allocate_candies(self, candies, k, num_of_candies):
26+
max_num_of_children = 0
27+
28+
for pile in candies:
29+
max_num_of_children += pile // num_of_candies
30+
31+
return max_num_of_children >= k

0 commit comments

Comments
 (0)