Skip to content

Commit 283baab

Browse files
authored
Create maximum-candies-allocated-to-k-children.py
1 parent e539ef1 commit 283baab

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Time: O(nlogr), r is max(candies)
2+
# Space: O(1)
3+
4+
# binary search
5+
class Solution(object):
6+
def maximumCandies(self, candies, k):
7+
"""
8+
:type candies: List[int]
9+
:type k: int
10+
:rtype: int
11+
"""
12+
def check(x):
13+
return sum(c//x for c in candies) >= k
14+
15+
left, right = 1, max(candies)
16+
while left <= right:
17+
mid = left+(right-left)//2
18+
if not check(mid):
19+
right = mid-1
20+
else:
21+
left = mid+1
22+
return right

0 commit comments

Comments
 (0)