Maximum Candies Allocated to K Children
Real-world version: Longest Lead Cut From The ReelsThe statement here is our own: the same inputs, outputs and technique.
A depot holds cable on reels, reels[i] being the whole number of metres left on reel i. An order calls for leads service leads, and every lead must be cut to the same whole number of metres.
A reel can be cut into as many leads as fit, and whatever is left over on it is scrap. A lead is one continuous run of cable, so it must come from a single reel: two short offcuts cannot be joined into one lead. Making more leads than the order asks for is fine, and reels may be left untouched.
Return the greatest length, in whole metres, that lets the order be filled. Return 0 when the depot cannot fill it even with one-metre leads.
Examples
Example 1
- Input
- reels = [16, 9, 27], leads = 7
- Output
- 6
At 6 metres the reels give 2, 1 and 4 leads, which is the 7 the order asks for. At 7 metres they give only 6.
Example 2
- Input
- reels = [4], leads = 5
- Output
- 0
The single reel holds 4 metres, so even one-metre leads come to 4, one short of the order.
Example 3
- Input
- reels = [100, 100], leads = 4
- Output
- 50
Each reel gives two 50-metre leads, filling the order of 4 exactly.
Constraints
- 1 <= reels.length <= 10^5
- 1 <= reels[i] <= 10^7
- 1 <= leads <= 10^12
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def longest_lead(reels: list[int], leads: int) -> int:public int longestLead(int[] reels, long leads)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 14 test cases: 14 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's