Cutting Ribbons
Real-world version: The Longest Equal Lengths From the ReelsThe statement here is our own: the same inputs, outputs and technique.
Reels of tape have the lengths reels. A reel may be cut into any number of pieces of whole-number lengths and any offcut may be thrown away, but pieces can never be joined together.
Obtain at least k pieces, all of the same whole-number length. Return the greatest length they can have, or 0 when not even a length of one can be managed.
Examples
Example 1
- Input
- reels = [9, 7, 5], k = 3
- Output
- 5
At a length of five each reel yields at least one piece, which is three in all. At six the reel of five yields nothing and the other two yield one apiece, leaving two pieces.
Example 2
- Input
- reels = [7, 5, 9], k = 4
- Output
- 4
At a length of four the reels yield one, one and two pieces, four in all. At five they yield one, one and one, which is one short.
Example 3
- Input
- reels = [5, 7, 9], k = 22
- Output
- 0
Even at a length of one the reels yield only twenty-one pieces between them, so no length can reach twenty-two.
Constraints
- 1 <= reels.length <= 10^5
- 1 <= reels[i] <= 10^5
- 1 <= k <= 10^9
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 max_length(reels: list[int], k: int) -> int:public int maxLength(int[] reels, int k)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 17 test cases: 17 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