Skip to content

Commit 4a76505

Browse files
committed
Maximum Candies Allocated to K Children
1 parent 903c68b commit 4a76505

File tree

3 files changed

+171
-3
lines changed

3 files changed

+171
-3
lines changed

179.largest_number/numbers.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ def largestNumber(self, nums: List[int]) -> str:
3434
def compare(x, y):
3535
if x + y > y + x:
3636
return -1
37-
elif x + y < y + x:
38-
return 1
39-
return 0
37+
return 1
4038
nums_str.sort(key=cmp_to_key(compare))
4139
if nums_str[0] == '0':
4240
return nums_str[0]
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# 2226. Maximum Candies Allocated to K Children
2+
# Topics: 'Array', 'Binary Search'
3+
# Level: 'Medium'
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+
19+
# Example 2:
20+
21+
# Input: candies = [2,5], k = 11
22+
# Output: 0
23+
# 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.
24+
25+
26+
27+
# Constraints:
28+
29+
# 1 <= candies.length <= 105
30+
# 1 <= candies[i] <= 107
31+
# 1 <= k <= 1012
32+
33+
from typing import List
34+
35+
class Solution:
36+
def maximumCandies(self, candies: List[int], k: int) -> int:
37+
if sum(candies) < k:
38+
return 0
39+
40+
maxx = max(candies)
41+
minn = 1
42+
res = 0
43+
while maxx >= minn:
44+
mid = minn + (maxx-minn) // 2
45+
times = 0
46+
for c in candies:
47+
if mid > c:
48+
continue
49+
times += c // mid
50+
if times >= k:
51+
break
52+
if times < k:
53+
maxx = mid-1
54+
else:
55+
res = mid
56+
minn = mid+1
57+
return res
58+
59+
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import unittest
2+
from typing import List
3+
from max import Solution
4+
5+
class TestMaximumCandies(unittest.TestCase):
6+
def setUp(self):
7+
self.solution = Solution()
8+
9+
def test_example_1(self):
10+
"""Example 1: Basic case with 3 children"""
11+
candies = [5, 8, 6]
12+
k = 3
13+
self.assertEqual(self.solution.maximumCandies(candies, k), 5)
14+
15+
def test_example_2(self):
16+
"""Example 2: Not enough candies"""
17+
candies = [2, 5]
18+
k = 11
19+
self.assertEqual(self.solution.maximumCandies(candies, k), 0)
20+
21+
def test_single_pile_single_child(self):
22+
"""Single pile, single child gets all"""
23+
candies = [10]
24+
k = 1
25+
self.assertEqual(self.solution.maximumCandies(candies, k), 10)
26+
27+
def test_single_pile_multiple_children(self):
28+
"""Single pile divided among multiple children"""
29+
candies = [20]
30+
k = 4
31+
self.assertEqual(self.solution.maximumCandies(candies, k), 5)
32+
33+
def test_equal_distribution(self):
34+
"""All piles same size, perfect division"""
35+
candies = [10, 10, 10, 10]
36+
k = 4
37+
self.assertEqual(self.solution.maximumCandies(candies, k), 10)
38+
39+
def test_one_child(self):
40+
"""Only one child, gets max pile"""
41+
candies = [1, 2, 3, 4, 5]
42+
k = 1
43+
self.assertEqual(self.solution.maximumCandies(candies, k), 5)
44+
45+
def test_exact_division(self):
46+
"""Piles divide exactly into k portions"""
47+
candies = [9, 12, 15]
48+
k = 12
49+
self.assertEqual(self.solution.maximumCandies(candies, k), 3)
50+
51+
def test_large_k(self):
52+
"""Very large k value (edge case from constraints)"""
53+
candies = [1, 2, 3]
54+
k = 1000000000000 # 10^12
55+
self.assertEqual(self.solution.maximumCandies(candies, k), 0)
56+
57+
def test_all_ones(self):
58+
"""All piles have size 1"""
59+
candies = [1, 1, 1, 1, 1]
60+
k = 5
61+
self.assertEqual(self.solution.maximumCandies(candies, k), 1)
62+
63+
def test_all_ones_too_many_children(self):
64+
"""All piles have size 1, but k is larger"""
65+
candies = [1, 1, 1]
66+
k = 5
67+
self.assertEqual(self.solution.maximumCandies(candies, k), 0)
68+
69+
def test_large_piles(self):
70+
"""Large pile values"""
71+
candies = [10000000, 10000000]
72+
k = 2
73+
self.assertEqual(self.solution.maximumCandies(candies, k), 10000000)
74+
75+
def test_cannot_divide_evenly(self):
76+
"""Candies cannot be divided evenly"""
77+
candies = [7, 11]
78+
k = 5
79+
self.assertEqual(self.solution.maximumCandies(candies, k), 3)
80+
81+
def test_single_large_pile(self):
82+
"""Single very large pile"""
83+
candies = [1000000]
84+
k = 100
85+
self.assertEqual(self.solution.maximumCandies(candies, k), 10000)
86+
87+
def test_zero_children_edge_case(self):
88+
"""Edge case: k = 1 (minimum from constraints)"""
89+
candies = [100]
90+
k = 1
91+
self.assertEqual(self.solution.maximumCandies(candies, k), 100)
92+
93+
94+
def run_tests():
95+
"""Run all tests and display results"""
96+
suite = unittest.TestLoader().loadTestsFromTestCase(TestMaximumCandies)
97+
runner = unittest.TextTestRunner(verbosity=2)
98+
result = runner.run(suite)
99+
100+
print("\n" + "="*70)
101+
print(f"Tests run: {result.testsRun}")
102+
print(f"Successes: {result.testsRun - len(result.failures) - len(result.errors)}")
103+
print(f"Failures: {len(result.failures)}")
104+
print(f"Errors: {len(result.errors)}")
105+
print("="*70)
106+
107+
return result.wasSuccessful()
108+
109+
110+
if __name__ == "__main__":
111+
run_tests()

0 commit comments

Comments
 (0)