Skip to content

Commit f606926

Browse files
committed
find alll
1 parent fb8880b commit f606926

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
3+
2226. Maximum Candies Allocated to K Children
4+
5+
You are given a 0-indexed integer array candies. Each element in the array denotes a pile of candies of size candies[i]. You can divide each pile into any number of sub piles, but you cannot merge two piles together.
6+
7+
You are also given an integer k. You should allocate piles of candies to k children such that each child gets the same number of candies. Each child can be allocated candies from only one pile of candies and some piles of candies may go unused.
8+
9+
Return the maximum number of candies each child can get.
10+
11+
12+
13+
Example 1:
14+
15+
Input: candies = [5,8,6], k = 3
16+
Output: 5
17+
Explanation: 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.
18+
Example 2:
19+
20+
Input: candies = [2,5], k = 11
21+
Output: 0
22+
Explanation: 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.
23+
24+
25+
Constraints:
26+
27+
1 <= candies.length <= 105
28+
1 <= candies[i] <= 107
29+
1 <= k <= 1012
30+
'''
31+
*/
32+
33+
class Solution:
34+
def maximumCandies(self, candies: List[int], k: int) -> int:
35+
left, right = 1, max(candies)
36+
result = 0
37+
38+
while left <= right:
39+
mid = (left + right) // 2
40+
41+
children_count = sum(pile // mid for pile in candies)
42+
43+
if children_count >= k:
44+
result = mid
45+
left = mid + 1
46+
else:
47+
right = mid - 1
48+
49+
return result
50+
51+
# Time complexity : O(nlogm) where n is the number of piles and m is the maximum number of candies in a pile.
52+
# Space complexity : O(1) as we are using only constant space.
53+
54+
// Java code
55+
class Solution {
56+
public int maximumCandies(int[] candies, int k) {
57+
int left = 1, right = Arrays.stream(candies).max().getAsInt();
58+
int result = 0;
59+
60+
while (left <= right) {
61+
int mid = (left + right) / 2;
62+
63+
int childrenCount = Arrays.stream(candies).mapToInt(candy -> candy / mid).sum();
64+
65+
if (childrenCount >= k) {
66+
result = mid;
67+
left = mid + 1;
68+
} else {
69+
right = mid - 1;
70+
}
71+
}
72+
73+
return result;
74+
}
75+
}
76+
77+
// Time complexity : O(nlogm) where n is the number of piles and m is the maximum number of candies in a pile.
78+
// Space complexity : O(1) as we are using only constant space.
79+
'''
80+
'''
81+
82+
from typing import List
83+
84+
class Solution:
85+
def maximumCandies(self, candies: List[int], k: int) -> int:
86+
left, right = 1, max(candies)
87+
result = 0
88+
while left <= right:
89+
mid = (left + right) // 2
90+
children_count = sum(pile // mid for pile in candies)
91+
if children_count >= k:
92+
result = mid
93+
left = mid + 1
94+
else:
95+
right = mid - 1
96+
return result
97+
'''

0 commit comments

Comments
 (0)