Skip to content

Commit 8c4bba2

Browse files
authored
feat: add solutions to lc problem: No.2929 (#4452)
No.2929. Distribute Candies Among Children II
1 parent c6b4e43 commit 8c4bba2

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

solution/2900-2999/2929.Distribute Candies Among Children II/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,30 @@ function distributeCandies(n: number, limit: number): number {
172172
}
173173
```
174174

175+
#### Rust
176+
177+
```rust
178+
impl Solution {
179+
pub fn distribute_candies(n: i32, limit: i32) -> i64 {
180+
if n > 3 * limit {
181+
return 0;
182+
}
183+
let mut ans = Self::comb2(n + 2);
184+
if n > limit {
185+
ans -= 3 * Self::comb2(n - limit + 1);
186+
}
187+
if n - 2 >= 2 * limit {
188+
ans += 3 * Self::comb2(n - 2 * limit);
189+
}
190+
ans
191+
}
192+
193+
fn comb2(n: i32) -> i64 {
194+
(n as i64) * (n as i64 - 1) / 2
195+
}
196+
}
197+
```
198+
175199
<!-- tabs:end -->
176200

177201
<!-- solution:end -->

solution/2900-2999/2929.Distribute Candies Among Children II/README_EN.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,30 @@ function distributeCandies(n: number, limit: number): number {
170170
}
171171
```
172172

173+
#### Rust
174+
175+
```rust
176+
impl Solution {
177+
pub fn distribute_candies(n: i32, limit: i32) -> i64 {
178+
if n > 3 * limit {
179+
return 0;
180+
}
181+
let mut ans = Self::comb2(n + 2);
182+
if n > limit {
183+
ans -= 3 * Self::comb2(n - limit + 1);
184+
}
185+
if n - 2 >= 2 * limit {
186+
ans += 3 * Self::comb2(n - 2 * limit);
187+
}
188+
ans
189+
}
190+
191+
fn comb2(n: i32) -> i64 {
192+
(n as i64) * (n as i64 - 1) / 2
193+
}
194+
}
195+
```
196+
173197
<!-- tabs:end -->
174198

175199
<!-- solution:end -->
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
impl Solution {
2+
pub fn distribute_candies(n: i32, limit: i32) -> i64 {
3+
if n > 3 * limit {
4+
return 0;
5+
}
6+
let mut ans = Self::comb2(n + 2);
7+
if n > limit {
8+
ans -= 3 * Self::comb2(n - limit + 1);
9+
}
10+
if n - 2 >= 2 * limit {
11+
ans += 3 * Self::comb2(n - 2 * limit);
12+
}
13+
ans
14+
}
15+
16+
fn comb2(n: i32) -> i64 {
17+
(n as i64) * (n as i64 - 1) / 2
18+
}
19+
}

0 commit comments

Comments
 (0)