Distribute Candies Among Children II
Real-world version: Splitting a Batch Across Three BinsThe statement here is our own: the same inputs, outputs and technique.
A batch of n identical parts is to be split across three bins, taken in order, and no bin may hold more than cap parts. A bin may be left empty.
Two splits count as different when some bin's count differs between them. Return how many splits there are.
Examples
Example 1
- Input
- n = 5, cap = 2
- Output
- 3
Five parts across three bins holding at most two each. The counts have to be some ordering of two, two and one, and there are three places the single part can go.
Example 2
- Input
- n = 3, cap = 3
- Output
- 10
Three parts with a cap of three means the cap never bites, so every way of writing three as an ordered triple counts: all in one bin, three ways; two and one, six ways; and one each, one way.
Example 3
- Input
- n = 10, cap = 2
- Output
- 0
Three bins of at most two hold six parts between them, which is short of ten, so no split works.
Constraints
- 1 <= n <= 10^6
- 1 <= cap <= 10^6
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 distribute_candies(n: int, cap: int) -> int:public long distributeCandies(int n, int cap)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 16 test cases: 16 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
More on the same topics
- Maximum Score with Co-Prime ElementReal-world version: Best Pick Plus What It Leaves CoprimeHard
- Smallest Divisible Digit Product IReal-world version: Next Shelf LabelEasy
- Count Square Sum TriplesReal-world version: Triples of Squares That Add UpEasy
- Unique PathsReal-world version: Grid Courier RoutesMedium