-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path129.py
More file actions
35 lines (28 loc) · 1.32 KB
/
129.py
File metadata and controls
35 lines (28 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Solution:
def maximumCandies(self, candies, k):
# Find the maximum number of candies in any pile
max_candies_in_pile = 0
for candy in candies:
max_candies_in_pile = max(max_candies_in_pile, candy)
# Set the initial search range for binary search
left = 0
right = max_candies_in_pile
# Binary search to find the maximum number of candies each child can get
while left < right:
# Calculate the middle value of the current range
middle = (left + right + 1) // 2
# Check if it's possible to allocate candies so that each child gets 'middle' candies
if self._can_allocate_candies(candies, k, middle):
# If possible, move to the upper half to search for a larger number
left = middle
else:
# Otherwise, move to the lower half
right = middle - 1
return left
def _can_allocate_candies(self, candies, k, num_of_candies):
# Initialize the total number of children that can be served
max_num_of_children = 0
# Iterate over all piles to calculate how many children each pile can serve
for pile in candies:
max_num_of_children += pile // num_of_candies
return max_num_of_children >= k