-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
feat: add solutions to lc problem: No.2927 #1945
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
167 changes: 167 additions & 0 deletions
solution/2900-2999/2927.Distribute Candies Among Children III/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
# [2927. Distribute Candies Among Children III](https://leetcode.cn/problems/distribute-candies-among-children-iii) | ||
|
||
[English Version](/solution/2900-2999/2927.Distribute%20Candies%20Among%20Children%20III/README_EN.md) | ||
|
||
## 题目描述 | ||
|
||
<!-- 这里写题目描述 --> | ||
|
||
<p>You are given two positive integers <code>n</code> and <code>limit</code>.</p> | ||
|
||
<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> | ||
|
||
<p> </p> | ||
<p><strong class="example">Example 1:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> n = 5, limit = 2 | ||
<strong>Output:</strong> 3 | ||
<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). | ||
</pre> | ||
|
||
<p><strong class="example">Example 2:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> n = 3, limit = 3 | ||
<strong>Output:</strong> 10 | ||
<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). | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= n <= 10<sup>8</sup></code></li> | ||
<li><code>1 <= limit <= 10<sup>8</sup></code></li> | ||
</ul> | ||
|
||
## 解法 | ||
|
||
<!-- 这里可写通用的实现逻辑 --> | ||
|
||
**方法一:组合数学 + 容斥原理** | ||
|
||
根据题目描述,我们需要将 $n$ 个糖果分给 3ドル$ 个小孩,每个小孩分到的糖果数在 $[0, limit]$ 之间。 | ||
|
||
这实际上等价于把 $n$ 个球放入 3ドル$ 个盒子中。由于盒子可以为空,我们可以再增加 3ドル$ 个虚拟球,然后再利用隔板法,即一共有 $n + 3$ 个球,我们在其中 $n + 3 - 1$ 个位置插入 2ドル$ 个隔板,从而将实际的 $n$ 个球分成 3ドル$ 组,并且允许盒子为空,因此初始方案数为 $C_{n + 2}^2$。 | ||
|
||
我们需要在这些方案中,排除掉存在盒子分到的小球数超过 $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$。 | ||
|
||
时间复杂度 $O(1),ドル空间复杂度 $O(1)$。 | ||
|
||
<!-- tabs:start --> | ||
|
||
### **Python3** | ||
|
||
<!-- 这里可写当前语言的特殊实现逻辑 --> | ||
|
||
```python | ||
class Solution: | ||
def distributeCandies(self, n: int, limit: int) -> int: | ||
if n > 3 * limit: | ||
return 0 | ||
ans = comb(n + 2, 2) | ||
if n > limit: | ||
ans -= 3 * comb(n - limit + 1, 2) | ||
if n - 2 >= 2 * limit: | ||
ans += 3 * comb(n - 2 * limit, 2) | ||
return ans | ||
``` | ||
|
||
### **Java** | ||
|
||
<!-- 这里可写当前语言的特殊实现逻辑 --> | ||
|
||
```java | ||
class Solution { | ||
public long distributeCandies(int n, int limit) { | ||
if (n > 3 * limit) { | ||
return 0; | ||
} | ||
long ans = comb2(n + 2); | ||
if (n > limit) { | ||
ans -= 3 * comb2(n - limit + 1); | ||
} | ||
if (n - 2 >= 2 * limit) { | ||
ans += 3 * comb2(n - 2 * limit); | ||
} | ||
return ans; | ||
} | ||
|
||
private long comb2(int n) { | ||
return 1L * n * (n - 1) / 2; | ||
} | ||
} | ||
``` | ||
|
||
### **C++** | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
long long distributeCandies(int n, int limit) { | ||
auto comb2 = [](int n) { | ||
return 1LL * n * (n - 1) / 2; | ||
}; | ||
if (n > 3 * limit) { | ||
return 0; | ||
} | ||
long long ans = comb2(n + 2); | ||
if (n > limit) { | ||
ans -= 3 * comb2(n - limit + 1); | ||
} | ||
if (n - 2 >= 2 * limit) { | ||
ans += 3 * comb2(n - 2 * limit); | ||
} | ||
return ans; | ||
} | ||
}; | ||
``` | ||
|
||
### **Go** | ||
|
||
```go | ||
func distributeCandies(n int, limit int) int64 { | ||
comb2 := func(n int) int { | ||
return n * (n - 1) / 2 | ||
} | ||
if n > 3*limit { | ||
return 0 | ||
} | ||
ans := comb2(n+2) | ||
if n > limit { | ||
ans -= 3 * comb2(n-limit+1) | ||
} | ||
if n-2 >= 2*limit { | ||
ans += 3 * comb2(n-2*limit) | ||
} | ||
return int64(ans) | ||
} | ||
``` | ||
|
||
### **TypeScript** | ||
|
||
```ts | ||
function distributeCandies(n: number, limit: number): number { | ||
const comb2 = (n: number) => (n * (n - 1)) / 2; | ||
if (n > 3 * limit) { | ||
return 0; | ||
} | ||
let ans = comb2(n + 2); | ||
if (n > limit) { | ||
ans -= 3 * comb2(n - limit + 1); | ||
} | ||
if (n - 2 >= 2 * limit) { | ||
ans += 3 * comb2(n - 2 * limit); | ||
} | ||
return ans; | ||
} | ||
``` | ||
|
||
### **...** | ||
|
||
``` | ||
|
||
``` | ||
|
||
<!-- tabs:end --> |
159 changes: 159 additions & 0 deletions
solution/2900-2999/2927.Distribute Candies Among Children III/README_EN.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
# [2927. Distribute Candies Among Children III](https://leetcode.com/problems/distribute-candies-among-children-iii) | ||
|
||
[中文文档](/solution/2900-2999/2927.Distribute%20Candies%20Among%20Children%20III/README.md) | ||
|
||
## Description | ||
|
||
<p>You are given two positive integers <code>n</code> and <code>limit</code>.</p> | ||
|
||
<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> | ||
|
||
<p> </p> | ||
<p><strong class="example">Example 1:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> n = 5, limit = 2 | ||
<strong>Output:</strong> 3 | ||
<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). | ||
</pre> | ||
|
||
<p><strong class="example">Example 2:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> n = 3, limit = 3 | ||
<strong>Output:</strong> 10 | ||
<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). | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= n <= 10<sup>8</sup></code></li> | ||
<li><code>1 <= limit <= 10<sup>8</sup></code></li> | ||
</ul> | ||
|
||
## Solutions | ||
|
||
**Solution 1: Combinatorial Mathematics + Principle of Inclusion-Exclusion** | ||
|
||
According to the problem description, we need to distribute $n$ candies to 3ドル$ children, with each child receiving between $[0, limit]$ candies. | ||
|
||
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$. | ||
|
||
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$. | ||
|
||
The time complexity is $O(1),ドル and the space complexity is $O(1)$. | ||
|
||
<!-- tabs:start --> | ||
|
||
### **Python3** | ||
|
||
```python | ||
class Solution: | ||
def distributeCandies(self, n: int, limit: int) -> int: | ||
if n > 3 * limit: | ||
return 0 | ||
ans = comb(n + 2, 2) | ||
if n > limit: | ||
ans -= 3 * comb(n - limit + 1, 2) | ||
if n - 2 >= 2 * limit: | ||
ans += 3 * comb(n - 2 * limit, 2) | ||
return ans | ||
``` | ||
|
||
### **Java** | ||
|
||
```java | ||
class Solution { | ||
public long distributeCandies(int n, int limit) { | ||
if (n > 3 * limit) { | ||
return 0; | ||
} | ||
long ans = comb2(n + 2); | ||
if (n > limit) { | ||
ans -= 3 * comb2(n - limit + 1); | ||
} | ||
if (n - 2 >= 2 * limit) { | ||
ans += 3 * comb2(n - 2 * limit); | ||
} | ||
return ans; | ||
} | ||
|
||
private long comb2(int n) { | ||
return 1L * n * (n - 1) / 2; | ||
} | ||
} | ||
``` | ||
|
||
### **C++** | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
long long distributeCandies(int n, int limit) { | ||
auto comb2 = [](int n) { | ||
return 1LL * n * (n - 1) / 2; | ||
}; | ||
if (n > 3 * limit) { | ||
return 0; | ||
} | ||
long long ans = comb2(n + 2); | ||
if (n > limit) { | ||
ans -= 3 * comb2(n - limit + 1); | ||
} | ||
if (n - 2 >= 2 * limit) { | ||
ans += 3 * comb2(n - 2 * limit); | ||
} | ||
return ans; | ||
} | ||
}; | ||
``` | ||
|
||
### **Go** | ||
|
||
```go | ||
func distributeCandies(n int, limit int) int64 { | ||
comb2 := func(n int) int { | ||
return n * (n - 1) / 2 | ||
} | ||
if n > 3*limit { | ||
return 0 | ||
} | ||
ans := comb2(n+2) | ||
if n > limit { | ||
ans -= 3 * comb2(n-limit+1) | ||
} | ||
if n-2 >= 2*limit { | ||
ans += 3 * comb2(n-2*limit) | ||
} | ||
return int64(ans) | ||
} | ||
``` | ||
|
||
### **TypeScript** | ||
|
||
```ts | ||
function distributeCandies(n: number, limit: number): number { | ||
const comb2 = (n: number) => (n * (n - 1)) / 2; | ||
if (n > 3 * limit) { | ||
return 0; | ||
} | ||
let ans = comb2(n + 2); | ||
if (n > limit) { | ||
ans -= 3 * comb2(n - limit + 1); | ||
} | ||
if (n - 2 >= 2 * limit) { | ||
ans += 3 * comb2(n - 2 * limit); | ||
} | ||
return ans; | ||
} | ||
``` | ||
|
||
### **...** | ||
|
||
``` | ||
|
||
``` | ||
|
||
<!-- tabs:end --> |
19 changes: 19 additions & 0 deletions
solution/2900-2999/2927.Distribute Candies Among Children III/Solution.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class Solution { | ||
public: | ||
long long distributeCandies(int n, int limit) { | ||
auto comb2 = [](int n) { | ||
return 1LL * n * (n - 1) / 2; | ||
}; | ||
if (n > 3 * limit) { | ||
return 0; | ||
} | ||
long long ans = comb2(n + 2); | ||
if (n > limit) { | ||
ans -= 3 * comb2(n - limit + 1); | ||
} | ||
if (n - 2 >= 2 * limit) { | ||
ans += 3 * comb2(n - 2 * limit); | ||
} | ||
return ans; | ||
} | ||
}; |
16 changes: 16 additions & 0 deletions
solution/2900-2999/2927.Distribute Candies Among Children III/Solution.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
func distributeCandies(n int, limit int) int64 { | ||
comb2 := func(n int) int { | ||
return n * (n - 1) / 2 | ||
} | ||
if n > 3*limit { | ||
return 0 | ||
} | ||
ans := comb2(n+2) | ||
if n > limit { | ||
ans -= 3 * comb2(n-limit+1) | ||
} | ||
if n-2 >= 2*limit { | ||
ans += 3 * comb2(n-2*limit) | ||
} | ||
return int64(ans) | ||
} |
19 changes: 19 additions & 0 deletions
solution/2900-2999/2927.Distribute Candies Among Children III/Solution.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class Solution { | ||
public long distributeCandies(int n, int limit) { | ||
if (n > 3 * limit) { | ||
return 0; | ||
} | ||
long ans = comb2(n + 2); | ||
if (n > limit) { | ||
ans -= 3 * comb2(n - limit + 1); | ||
} | ||
if (n - 2 >= 2 * limit) { | ||
ans += 3 * comb2(n - 2 * limit); | ||
} | ||
return ans; | ||
} | ||
|
||
private long comb2(int n) { | ||
return 1L * n * (n - 1) / 2; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
solution/2900-2999/2927.Distribute Candies Among Children III/Solution.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class Solution: | ||
def distributeCandies(self, n: int, limit: int) -> int: | ||
if n > 3 * limit: | ||
return 0 | ||
ans = comb(n + 2, 2) | ||
if n > limit: | ||
ans -= 3 * comb(n - limit + 1, 2) | ||
if n - 2 >= 2 * limit: | ||
ans += 3 * comb(n - 2 * limit, 2) | ||
return ans |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.