Distribute Candies Among Children I
Problem
You are given two positive integers n and limit.
Return the total number of ways to distribute n candies among 3 children such that no child gets more than limit candies.
Examples
Example 1:
Input: n = 5, limit = 2
Output: 3
Explanation: There are 3 ways to distribute 5 candies such that no child gets more than 2 candies: (1, 2, 2), (2, 1, 2) and (2, 2, 1).
Example 2:
Input: n = 3, limit = 3
Output: 10
Explanation: There are 10 ways to distribute 3 candies such that no child gets more than 3 candies: (0, 0, 3), (0, 1, 2), (0, 2, 1), (0, 3, 0), (1, 0, 2), (1, 1, 1), (1, 2, 0), (2, 0, 1), (2, 1, 0) and (3, 0, 0).
Constraints:
1 <= n <= 501 <= limit <= 50
Similar Problems
[Distribute Candies Among Children II](distribute-candies-among-children-ii) [Distribute Candies Among Children III](distribute-candies-among-children-iii)
Solution
Method 1 – Inclusion-Exclusion Principle
Intuition
The problem asks for the number of ways to distribute n candies among 3 children, with each child getting at most limit candies. If there were no upper limit, the answer would be the number of non-negative integer solutions to a + b + c = n, which is C(n+3-1, 3-1) = C(n+2, 2). The limit constraint can be handled using the inclusion-exclusion principle: subtract cases where one or more children exceed the limit.
Approach
- Count all non-negative solutions to
a + b + c = n(no limits): use the stars and bars formula. - Subtract cases where at least one child gets more than
limitcandies:
- For each subset of children, use inclusion-exclusion to add or subtract the number of ways where those children get at least
limit+1candies.
- For each subset (represented by a bitmask of 3 bits), calculate the reduced candies after assigning at least
limit+1to selected children. - Sum up the contributions with the correct sign (add or subtract based on subset size).
Code
C++
class Solution {
public:
int distributeCandies(int n, int limit) {
int ans = 0;
for (int mask = 0; mask < 8; ++mask) {
int cnt = 0, rem = n;
for (int i = 0; i < 3; ++i) {
if (mask & (1 << i)) {
rem -= (limit + 1);
cnt++;
}
}
if (rem < 0) continue;
int ways = (rem + 2) * (rem + 1) / 2;
ans += (cnt % 2 == 0 ? ways : -ways);
}
return ans;
}
};
Java
class Solution {
public int distributeCandies(int n, int limit) {
int ans = 0;
for (int mask = 0; mask < 8; ++mask) {
int cnt = 0, rem = n;
for (int i = 0; i < 3; ++i) {
if ((mask & (1 << i)) != 0) {
rem -= (limit + 1);
cnt++;
}
}
if (rem < 0) continue;
int ways = (rem + 2) * (rem + 1) / 2;
ans += (cnt % 2 == 0 ? ways : -ways);
}
return ans;
}
}
Python
class Solution:
def distributeCandies(self, n: int, limit: int) -> int:
ans = 0
for mask in range(8):
cnt = 0
rem = n
for i in range(3):
if (mask >> i) & 1:
rem -= (limit + 1)
cnt += 1
if rem < 0:
continue
ways = (rem + 2) * (rem + 1) // 2
ans += ways if cnt % 2 == 0 else -ways
return ans
Complexity
- ⏰ Time complexity:
O(1)(since 3 children, only 8 subsets) - 🧺 Space complexity:
O(1)