From 00ed584410d15409134faaacc205fff713595be4 Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Thu, 9 Nov 2023 04:57:12 +0000 Subject: [PATCH] feat: add solutions to lc problem: No.2927 No.2927.Distribute Candies Among Children III --- .../README.md | 167 ++++++++++++++++++ .../README_EN.md | 159 +++++++++++++++++ .../Solution.cpp | 19 ++ .../Solution.go | 16 ++ .../Solution.java | 19 ++ .../Solution.py | 10 ++ .../Solution.ts | 14 ++ solution/README.md | 1 + solution/README_EN.md | 1 + solution/summary.md | 1 + solution/summary_en.md | 1 + 11 files changed, 408 insertions(+) create mode 100644 solution/2900-2999/2927.Distribute Candies Among Children III/README.md create mode 100644 solution/2900-2999/2927.Distribute Candies Among Children III/README_EN.md create mode 100644 solution/2900-2999/2927.Distribute Candies Among Children III/Solution.cpp create mode 100644 solution/2900-2999/2927.Distribute Candies Among Children III/Solution.go create mode 100644 solution/2900-2999/2927.Distribute Candies Among Children III/Solution.java create mode 100644 solution/2900-2999/2927.Distribute Candies Among Children III/Solution.py create mode 100644 solution/2900-2999/2927.Distribute Candies Among Children III/Solution.ts diff --git a/solution/2900-2999/2927.Distribute Candies Among Children III/README.md b/solution/2900-2999/2927.Distribute Candies Among Children III/README.md new file mode 100644 index 0000000000000..a5ff2396e9b49 --- /dev/null +++ b/solution/2900-2999/2927.Distribute Candies Among Children III/README.md @@ -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) + +## 题目描述 + + + +

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.

+ +

 

+

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:

+ + + +## 解法 + + + +**方法一:组合数学 + 容斥原理** + +根据题目描述,我们需要将 $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)$。 + + + +### **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; +} +``` + +### **...** + +``` + +``` + + diff --git a/solution/2900-2999/2927.Distribute Candies Among Children III/README_EN.md b/solution/2900-2999/2927.Distribute Candies Among Children III/README_EN.md new file mode 100644 index 0000000000000..aa0b6dd2054ee --- /dev/null +++ b/solution/2900-2999/2927.Distribute Candies Among Children III/README_EN.md @@ -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 + +

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.

+ +

 

+

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:

+ + + +## 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)$. + + + +### **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; +} +``` + +### **...** + +``` + +``` + + diff --git a/solution/2900-2999/2927.Distribute Candies Among Children III/Solution.cpp b/solution/2900-2999/2927.Distribute Candies Among Children III/Solution.cpp new file mode 100644 index 0000000000000..008d72255bb8e --- /dev/null +++ b/solution/2900-2999/2927.Distribute Candies Among Children III/Solution.cpp @@ -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; + } +}; \ No newline at end of file diff --git a/solution/2900-2999/2927.Distribute Candies Among Children III/Solution.go b/solution/2900-2999/2927.Distribute Candies Among Children III/Solution.go new file mode 100644 index 0000000000000..7705fb2057848 --- /dev/null +++ b/solution/2900-2999/2927.Distribute Candies Among Children III/Solution.go @@ -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) +} \ No newline at end of file diff --git a/solution/2900-2999/2927.Distribute Candies Among Children III/Solution.java b/solution/2900-2999/2927.Distribute Candies Among Children III/Solution.java new file mode 100644 index 0000000000000..68684d6c5f5b5 --- /dev/null +++ b/solution/2900-2999/2927.Distribute Candies Among Children III/Solution.java @@ -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; + } +} \ No newline at end of file diff --git a/solution/2900-2999/2927.Distribute Candies Among Children III/Solution.py b/solution/2900-2999/2927.Distribute Candies Among Children III/Solution.py new file mode 100644 index 0000000000000..f08cfa1c4f041 --- /dev/null +++ b/solution/2900-2999/2927.Distribute Candies Among Children III/Solution.py @@ -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 diff --git a/solution/2900-2999/2927.Distribute Candies Among Children III/Solution.ts b/solution/2900-2999/2927.Distribute Candies Among Children III/Solution.ts new file mode 100644 index 0000000000000..39744f07459a0 --- /dev/null +++ b/solution/2900-2999/2927.Distribute Candies Among Children III/Solution.ts @@ -0,0 +1,14 @@ +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; +} diff --git a/solution/README.md b/solution/README.md index d0bad30aff481..8a749e67a2299 100644 --- a/solution/README.md +++ b/solution/README.md @@ -2937,6 +2937,7 @@ | 2924 | [找到冠军 II](/solution/2900-2999/2924.Find%20Champion%20II/README.md) | | 中等 | 第 370 场周赛 | | 2925 | [在树上执行操作以后得到的最大分数](/solution/2900-2999/2925.Maximum%20Score%20After%20Applying%20Operations%20on%20a%20Tree/README.md) | | 中等 | 第 370 场周赛 | | 2926 | [平衡子序列的最大和](/solution/2900-2999/2926.Maximum%20Balanced%20Subsequence%20Sum/README.md) | | 困难 | 第 370 场周赛 | +| 2927 | [Distribute Candies Among Children III](/solution/2900-2999/2927.Distribute%20Candies%20Among%20Children%20III/README.md) | | 困难 | 🔒 | ## 版权 diff --git a/solution/README_EN.md b/solution/README_EN.md index f0ff03da0eabd..8f0ba402e5b2b 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -2935,6 +2935,7 @@ Press Control + F(or Command + F on | 2924 | [Find Champion II](/solution/2900-2999/2924.Find%20Champion%20II/README_EN.md) | | Medium | Weekly Contest 370 | | 2925 | [Maximum Score After Applying Operations on a Tree](/solution/2900-2999/2925.Maximum%20Score%20After%20Applying%20Operations%20on%20a%20Tree/README_EN.md) | | Medium | Weekly Contest 370 | | 2926 | [Maximum Balanced Subsequence Sum](/solution/2900-2999/2926.Maximum%20Balanced%20Subsequence%20Sum/README_EN.md) | | Hard | Weekly Contest 370 | +| 2927 | [Distribute Candies Among Children III](/solution/2900-2999/2927.Distribute%20Candies%20Among%20Children%20III/README_EN.md) | | Hard | 🔒 | ## Copyright diff --git a/solution/summary.md b/solution/summary.md index 6bb53362a86e9..e83950ae7d33e 100644 --- a/solution/summary.md +++ b/solution/summary.md @@ -2984,3 +2984,4 @@ - [2924.找到冠军 II](/solution/2900-2999/2924.Find%20Champion%20II/README.md) - [2925.在树上执行操作以后得到的最大分数](/solution/2900-2999/2925.Maximum%20Score%20After%20Applying%20Operations%20on%20a%20Tree/README.md) - [2926.平衡子序列的最大和](/solution/2900-2999/2926.Maximum%20Balanced%20Subsequence%20Sum/README.md) + - [2927.Distribute Candies Among Children III](/solution/2900-2999/2927.Distribute%20Candies%20Among%20Children%20III/README.md) diff --git a/solution/summary_en.md b/solution/summary_en.md index a71aa30f7215c..d07b738f89598 100644 --- a/solution/summary_en.md +++ b/solution/summary_en.md @@ -2984,3 +2984,4 @@ - [2924.Find Champion II](/solution/2900-2999/2924.Find%20Champion%20II/README_EN.md) - [2925.Maximum Score After Applying Operations on a Tree](/solution/2900-2999/2925.Maximum%20Score%20After%20Applying%20Operations%20on%20a%20Tree/README_EN.md) - [2926.Maximum Balanced Subsequence Sum](/solution/2900-2999/2926.Maximum%20Balanced%20Subsequence%20Sum/README_EN.md) + - [2927.Distribute Candies Among Children III](/solution/2900-2999/2927.Distribute%20Candies%20Among%20Children%20III/README_EN.md)