|
| 1 | +# [2927. Distribute Candies Among Children III](https://leetcode.com/problems/distribute-candies-among-children-iii) |
| 2 | + |
| 3 | +[中文文档](/solution/2900-2999/2927.Distribute%20Candies%20Among%20Children%20III/README.md) |
| 4 | + |
| 5 | +## Description |
| 6 | + |
| 7 | +<p>You are given two positive integers <code>n</code> and <code>limit</code>.</p> |
| 8 | + |
| 9 | +<p>Return <em>the <strong>total number</strong> of ways to distribute </em><code>n</code> <em>candies among </em><code>3</code><em> children such that no child gets more than </em><code>limit</code><em> candies.</em></p> |
| 10 | + |
| 11 | +<p> </p> |
| 12 | +<p><strong class="example">Example 1:</strong></p> |
| 13 | + |
| 14 | +<pre> |
| 15 | +<strong>Input:</strong> n = 5, limit = 2 |
| 16 | +<strong>Output:</strong> 3 |
| 17 | +<strong>Explanation:</strong> 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). |
| 18 | +</pre> |
| 19 | + |
| 20 | +<p><strong class="example">Example 2:</strong></p> |
| 21 | + |
| 22 | +<pre> |
| 23 | +<strong>Input:</strong> n = 3, limit = 3 |
| 24 | +<strong>Output:</strong> 10 |
| 25 | +<strong>Explanation:</strong> 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). |
| 26 | +</pre> |
| 27 | + |
| 28 | +<p> </p> |
| 29 | +<p><strong>Constraints:</strong></p> |
| 30 | + |
| 31 | +<ul> |
| 32 | + <li><code>1 <= n <= 10<sup>8</sup></code></li> |
| 33 | + <li><code>1 <= limit <= 10<sup>8</sup></code></li> |
| 34 | +</ul> |
| 35 | + |
| 36 | +## Solutions |
| 37 | + |
| 38 | +**Solution 1: Combinatorial Mathematics + Principle of Inclusion-Exclusion** |
| 39 | + |
| 40 | +According to the problem description, we need to distribute $n$ candies to $3$ children, with each child receiving between $[0, limit]$ candies. |
| 41 | + |
| 42 | +This is equivalent to placing $n$ balls into $3$ boxes. Since the boxes can be empty, we can add $3$ virtual balls, and then use the method of inserting partitions, i.e., there are a total of $n + 3$ balls, and we insert $2$ partitions among the $n + 3 - 1$ positions, thus dividing the actual $n$ balls into $3$ groups, and allowing the boxes to be empty. Therefore, the initial number of schemes is $C_{n + 2}^2$. |
| 43 | + |
| 44 | +We need to exclude the schemes where the number of balls in a box exceeds $limit$. Consider that there is a box where the number of balls exceeds $limit$, then the remaining balls (including virtual balls) have at most $n + 3 - (limit + 1) = n - limit + 2$, and the number of positions is $n - limit + 1$, so the number of schemes is $C_{n - limit + 1}^2$. Since there are $3$ boxes, the number of such schemes is $3 \times C_{n - limit + 1}^2$. In this way, we will exclude too many schemes where the number of balls in two boxes exceeds $limit$ at the same time, so we need to add the number of such schemes, i.e., $3 \times C_{n - 2 \times limit}^2$. |
| 45 | + |
| 46 | +The time complexity is $O(1)$, and the space complexity is $O(1)$. |
| 47 | + |
| 48 | +<!-- tabs:start --> |
| 49 | + |
| 50 | +### **Python3** |
| 51 | + |
| 52 | +```python |
| 53 | +class Solution: |
| 54 | + def distributeCandies(self, n: int, limit: int) -> int: |
| 55 | + if n > 3 * limit: |
| 56 | + return 0 |
| 57 | + ans = comb(n + 2, 2) |
| 58 | + if n > limit: |
| 59 | + ans -= 3 * comb(n - limit + 1, 2) |
| 60 | + if n - 2 >= 2 * limit: |
| 61 | + ans += 3 * comb(n - 2 * limit, 2) |
| 62 | + return ans |
| 63 | +``` |
| 64 | + |
| 65 | +### **Java** |
| 66 | + |
| 67 | +```java |
| 68 | +class Solution { |
| 69 | + public long distributeCandies(int n, int limit) { |
| 70 | + if (n > 3 * limit) { |
| 71 | + return 0; |
| 72 | + } |
| 73 | + long ans = comb2(n + 2); |
| 74 | + if (n > limit) { |
| 75 | + ans -= 3 * comb2(n - limit + 1); |
| 76 | + } |
| 77 | + if (n - 2 >= 2 * limit) { |
| 78 | + ans += 3 * comb2(n - 2 * limit); |
| 79 | + } |
| 80 | + return ans; |
| 81 | + } |
| 82 | + |
| 83 | + private long comb2(int n) { |
| 84 | + return 1L * n * (n - 1) / 2; |
| 85 | + } |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +### **C++** |
| 90 | + |
| 91 | +```cpp |
| 92 | +class Solution { |
| 93 | +public: |
| 94 | + long long distributeCandies(int n, int limit) { |
| 95 | + auto comb2 = [](int n) { |
| 96 | + return 1LL * n * (n - 1) / 2; |
| 97 | + }; |
| 98 | + if (n > 3 * limit) { |
| 99 | + return 0; |
| 100 | + } |
| 101 | + long long ans = comb2(n + 2); |
| 102 | + if (n > limit) { |
| 103 | + ans -= 3 * comb2(n - limit + 1); |
| 104 | + } |
| 105 | + if (n - 2 >= 2 * limit) { |
| 106 | + ans += 3 * comb2(n - 2 * limit); |
| 107 | + } |
| 108 | + return ans; |
| 109 | + } |
| 110 | +}; |
| 111 | +``` |
| 112 | +
|
| 113 | +### **Go** |
| 114 | +
|
| 115 | +```go |
| 116 | +func distributeCandies(n int, limit int) int64 { |
| 117 | + comb2 := func(n int) int { |
| 118 | + return n * (n - 1) / 2 |
| 119 | + } |
| 120 | + if n > 3*limit { |
| 121 | + return 0 |
| 122 | + } |
| 123 | + ans := comb2(n+2) |
| 124 | + if n > limit { |
| 125 | + ans -= 3 * comb2(n-limit+1) |
| 126 | + } |
| 127 | + if n-2 >= 2*limit { |
| 128 | + ans += 3 * comb2(n-2*limit) |
| 129 | + } |
| 130 | + return int64(ans) |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +### **TypeScript** |
| 135 | + |
| 136 | +```ts |
| 137 | +function distributeCandies(n: number, limit: number): number { |
| 138 | + const comb2 = (n: number) => (n * (n - 1)) / 2; |
| 139 | + if (n > 3 * limit) { |
| 140 | + return 0; |
| 141 | + } |
| 142 | + let ans = comb2(n + 2); |
| 143 | + if (n > limit) { |
| 144 | + ans -= 3 * comb2(n - limit + 1); |
| 145 | + } |
| 146 | + if (n - 2 >= 2 * limit) { |
| 147 | + ans += 3 * comb2(n - 2 * limit); |
| 148 | + } |
| 149 | + return ans; |
| 150 | +} |
| 151 | +``` |
| 152 | + |
| 153 | +### **...** |
| 154 | + |
| 155 | +``` |
| 156 | +
|
| 157 | +``` |
| 158 | + |
| 159 | +<!-- tabs:end --> |
0 commit comments