Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f5f8de8

Browse files
feat: add solutions to lc problems: No.2928,2929 (doocs#1956)
* No.2928.Distribute Candies Among Children I * No.2929.Distribute Candies Among Children II
1 parent d3da124 commit f5f8de8

File tree

14 files changed

+524
-12
lines changed

14 files changed

+524
-12
lines changed

‎solution/2900-2999/2928.Distribute Candies Among Children I/README.md‎

Lines changed: 92 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,34 +41,123 @@
4141

4242
<!-- 这里可写通用的实现逻辑 -->
4343

44+
**方法一:组合数学 + 容斥原理**
45+
46+
根据题目描述,我们需要将 $n$ 个糖果分给 3ドル$ 个小孩,每个小孩分到的糖果数在 $[0, limit]$ 之间。
47+
48+
这实际上等价于把 $n$ 个球放入 3ドル$ 个盒子中。由于盒子可以为空,我们可以再增加 3ドル$ 个虚拟球,然后再利用隔板法,即一共有 $n + 3$ 个球,我们在其中 $n + 3 - 1$ 个位置插入 2ドル$ 个隔板,从而将实际的 $n$ 个球分成 3ドル$ 组,并且允许盒子为空,因此初始方案数为 $C_{n + 2}^2$。
49+
50+
我们需要在这些方案中,排除掉存在盒子分到的小球数超过 $limit$ 的方案。考虑其中有一个盒子分到的小球数超过 $limit,ドル那么剩下的球(包括虚拟球)最多有 $n + 3 - (limit + 1) = n - limit + 2$ 个,位置数为 $n - limit + 1,ドル因此方案数为 $C_{n - limit + 1}^2$。由于存在 3ドル$ 个盒子,因此这样的方案数为 3ドル \times C_{n - limit + 1}^2$。这样子算,我们会多排除掉同时存在两个盒子分到的小球数超过 $limit$ 的方案,因此我们需要再加上这样的方案数,即 3ドル \times C_{n - 2 \times limit}^2$。
51+
52+
时间复杂度 $O(1),ドル空间复杂度 $O(1)$。
53+
4454
<!-- tabs:start -->
4555

4656
### **Python3**
4757

4858
<!-- 这里可写当前语言的特殊实现逻辑 -->
4959

5060
```python
51-
61+
class Solution:
62+
def distributeCandies(self, n: int, limit: int) -> int:
63+
if n > 3 * limit:
64+
return 0
65+
ans = comb(n + 2, 2)
66+
if n > limit:
67+
ans -= 3 * comb(n - limit + 1, 2)
68+
if n - 2 >= 2 * limit:
69+
ans += 3 * comb(n - 2 * limit, 2)
70+
return ans
5271
```
5372

5473
### **Java**
5574

5675
<!-- 这里可写当前语言的特殊实现逻辑 -->
5776

5877
```java
59-
78+
class Solution {
79+
public int distributeCandies(int n, int limit) {
80+
if (n > 3 * limit) {
81+
return 0;
82+
}
83+
long ans = comb2(n + 2);
84+
if (n > limit) {
85+
ans -= 3 * comb2(n - limit + 1);
86+
}
87+
if (n - 2 >= 2 * limit) {
88+
ans += 3 * comb2(n - 2 * limit);
89+
}
90+
return (int) ans;
91+
}
92+
93+
private long comb2(int n) {
94+
return 1L * n * (n - 1) / 2;
95+
}
96+
}
6097
```
6198

6299
### **C++**
63100

64101
```cpp
65-
102+
class Solution {
103+
public:
104+
int distributeCandies(int n, int limit) {
105+
auto comb2 = [](int n) {
106+
return 1LL * n * (n - 1) / 2;
107+
};
108+
if (n > 3 * limit) {
109+
return 0;
110+
}
111+
long long ans = comb2(n + 2);
112+
if (n > limit) {
113+
ans -= 3 * comb2(n - limit + 1);
114+
}
115+
if (n - 2 >= 2 * limit) {
116+
ans += 3 * comb2(n - 2 * limit);
117+
}
118+
return ans;
119+
}
120+
};
66121
```
67122
68123
### **Go**
69124
70125
```go
126+
func distributeCandies(n int, limit int) int {
127+
comb2 := func(n int) int {
128+
return n * (n - 1) / 2
129+
}
130+
if n > 3*limit {
131+
return 0
132+
}
133+
ans := comb2(n + 2)
134+
if n > limit {
135+
ans -= 3 * comb2(n-limit+1)
136+
}
137+
if n-2 >= 2*limit {
138+
ans += 3 * comb2(n-2*limit)
139+
}
140+
return ans
141+
}
142+
```
71143

144+
### **TypeScript**
145+
146+
```ts
147+
function distributeCandies(n: number, limit: number): number {
148+
const comb2 = (n: number) => (n * (n - 1)) / 2;
149+
if (n > 3 * limit) {
150+
return 0;
151+
}
152+
let ans = comb2(n + 2);
153+
if (n > limit) {
154+
ans -= 3 * comb2(n - limit + 1);
155+
}
156+
if (n - 2 >= 2 * limit) {
157+
ans += 3 * comb2(n - 2 * limit);
158+
}
159+
return ans;
160+
}
72161
```
73162

74163
### **...**

‎solution/2900-2999/2928.Distribute Candies Among Children I/README_EN.md‎

Lines changed: 92 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,30 +35,119 @@
3535

3636
## Solutions
3737

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+
3848
<!-- tabs:start -->
3949

4050
### **Python3**
4151

4252
```python
43-
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
4463
```
4564

4665
### **Java**
4766

4867
```java
49-
68+
class Solution {
69+
public int 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 (int) ans;
81+
}
82+
83+
private long comb2(int n) {
84+
return 1L * n * (n - 1) / 2;
85+
}
86+
}
5087
```
5188

5289
### **C++**
5390

5491
```cpp
55-
92+
class Solution {
93+
public:
94+
int 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+
};
56111
```
57112
58113
### **Go**
59114
60115
```go
116+
func distributeCandies(n int, limit int) int {
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 ans
131+
}
132+
```
61133

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+
}
62151
```
63152

64153
### **...**
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int distributeCandies(int n, int limit) {
4+
auto comb2 = [](int n) {
5+
return 1LL * n * (n - 1) / 2;
6+
};
7+
if (n > 3 * limit) {
8+
return 0;
9+
}
10+
long long ans = comb2(n + 2);
11+
if (n > limit) {
12+
ans -= 3 * comb2(n - limit + 1);
13+
}
14+
if (n - 2 >= 2 * limit) {
15+
ans += 3 * comb2(n - 2 * limit);
16+
}
17+
return ans;
18+
}
19+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
func distributeCandies(n int, limit int) int {
2+
comb2 := func(n int) int {
3+
return n * (n - 1) / 2
4+
}
5+
if n > 3*limit {
6+
return 0
7+
}
8+
ans := comb2(n + 2)
9+
if n > limit {
10+
ans -= 3 * comb2(n-limit+1)
11+
}
12+
if n-2 >= 2*limit {
13+
ans += 3 * comb2(n-2*limit)
14+
}
15+
return ans
16+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int distributeCandies(int n, int limit) {
3+
if (n > 3 * limit) {
4+
return 0;
5+
}
6+
long ans = comb2(n + 2);
7+
if (n > limit) {
8+
ans -= 3 * comb2(n - limit + 1);
9+
}
10+
if (n - 2 >= 2 * limit) {
11+
ans += 3 * comb2(n - 2 * limit);
12+
}
13+
return (int) ans;
14+
}
15+
16+
private long comb2(int n) {
17+
return 1L * n * (n - 1) / 2;
18+
}
19+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def distributeCandies(self, n: int, limit: int) -> int:
3+
if n > 3 * limit:
4+
return 0
5+
ans = comb(n + 2, 2)
6+
if n > limit:
7+
ans -= 3 * comb(n - limit + 1, 2)
8+
if n - 2 >= 2 * limit:
9+
ans += 3 * comb(n - 2 * limit, 2)
10+
return ans
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function distributeCandies(n: number, limit: number): number {
2+
const comb2 = (n: number) => (n * (n - 1)) / 2;
3+
if (n > 3 * limit) {
4+
return 0;
5+
}
6+
let ans = comb2(n + 2);
7+
if (n > limit) {
8+
ans -= 3 * comb2(n - limit + 1);
9+
}
10+
if (n - 2 >= 2 * limit) {
11+
ans += 3 * comb2(n - 2 * limit);
12+
}
13+
return ans;
14+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /