Skip to content

Commit 766020f

Browse files
authored
Merge pull request #411 from LetMeFly666/2928
添加问题“2928.给小朋友们分糖果I”的代码和题解
2 parents ed11a37 + 03e5ad2 commit 766020f

6 files changed

+205
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-06-01 11:42:17
4+
* @LastEditors: LetMeFly
5+
* @LastEditTime: 2024-06-01 11:43:37
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int distributeCandies(int n, int limit) {
14+
int ans = 0;
15+
for (int x = 0; x <= n && x <= limit; x++) {
16+
for (int y = 0; y <= n - x && y <= limit; y++) {
17+
if (n - x - y <= limit) {
18+
ans++;
19+
}
20+
}
21+
}
22+
return ans;
23+
}
24+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-06-01 11:48:10
4+
* @LastEditors: LetMeFly
5+
* @LastEditTime: 2024-06-01 11:49:32
6+
*/
7+
package main
8+
9+
func distributeCandies(n int, limit int) int {
10+
ans := 0
11+
for x := 0; x <= n && x <= limit; x++ {
12+
for y := 0; y <= n - x && y <= limit; y++ {
13+
if n - x - y <= limit {
14+
ans++
15+
}
16+
}
17+
}
18+
return ans
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-06-01 11:46:04
4+
* @LastEditors: LetMeFly
5+
* @LastEditTime: 2024-06-01 11:47:27
6+
*/
7+
class Solution {
8+
public int distributeCandies(int n, int limit) {
9+
int ans = 0;
10+
for (int x = 0; x <= n && x <= limit; x++) {
11+
for (int y = 0; y <= n - x && y <= limit; y++) {
12+
if (n - x - y <= limit) {
13+
ans++;
14+
}
15+
}
16+
}
17+
return ans;
18+
}
19+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2024-06-01 11:44:10
4+
LastEditors: LetMeFly
5+
LastEditTime: 2024-06-01 11:45:32
6+
'''
7+
class Solution:
8+
def distributeCandies(self, n: int, limit: int) -> int:
9+
ans = 0
10+
for x in range(min(limit, n) + 1):
11+
for y in range(min(n - x, limit) + 1):
12+
if n - x - y <= limit:
13+
ans += 1
14+
return ans

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,7 @@
615615
|2917.找出数组中的K-or值|简单|<a href="https://leetcode.cn/problems/find-the-k-or-of-an-array/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/03/06/LeetCode%202917.%E6%89%BE%E5%87%BA%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9A%84K-or%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/136497896" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-k-or-of-an-array/solutions/2670279/letmefly-2917zhao-chu-shu-zu-zhong-de-k-4wuuh/" target="_blank">LeetCode题解</a>|
616616
|2923.找到冠军I|简单|<a href="https://leetcode.cn/problems/find-champion-i/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/04/12/LeetCode%202923.%E6%89%BE%E5%88%B0%E5%86%A0%E5%86%9BI/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/137678593" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-champion-i/solutions/2736315/letmefly-2923zhao-dao-guan-jun-ion2he-on-jrxw/" target="_blank">LeetCode题解</a>|
617617
|2924.找到冠军II|中等|<a href="https://leetcode.cn/problems/find-champion-ii/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/04/13/LeetCode%202924.%E6%89%BE%E5%88%B0%E5%86%A0%E5%86%9BII/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/137707389" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-champion-ii/solutions/2737408/letmefly-2924zhao-dao-guan-jun-iinao-jin-miks/" target="_blank">LeetCode题解</a>|
618+
|2928.给小朋友们分糖果I|简单|<a href="https://leetcode.cn/problems/distribute-candies-among-children-i/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/06/01/LeetCode%202928.%E7%BB%99%E5%B0%8F%E6%9C%8B%E5%8F%8B%E4%BB%AC%E5%88%86%E7%B3%96%E6%9E%9CI/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139380754" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/distribute-candies-among-children-i/solutions/2797856/letmefly-2928gei-xiao-peng-you-men-fen-t-u01r/" target="_blank">LeetCode题解</a>|
618619
|2951.找出峰值|简单|<a href="https://leetcode.cn/problems/find-the-peaks/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/05/28/LeetCode%202951.%E6%89%BE%E5%87%BA%E5%B3%B0%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139279605" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-peaks/solutions/2793708/letmefly-2951zhao-chu-feng-zhi-mo-ni-bia-5jn1/" target="_blank">LeetCode题解</a>|
619620
|2952.需要添加的硬币的最小数量|中等|<a href="https://leetcode.cn/problems/minimum-number-of-coins-to-be-added/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/03/30/LeetCode%202952.%E9%9C%80%E8%A6%81%E6%B7%BB%E5%8A%A0%E7%9A%84%E7%A1%AC%E5%B8%81%E7%9A%84%E6%9C%80%E5%B0%8F%E6%95%B0%E9%87%8F/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/137185903" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-number-of-coins-to-be-added/solutions/2716158/letmefly-2952xu-yao-tian-jia-de-ying-bi-bxfa4/" target="_blank">LeetCode题解</a>|
620621
|2960.统计已测试设备|简单|<a href="https://leetcode.cn/problems/count-tested-devices-after-test-operations/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/05/10/LeetCode%202960.%E7%BB%9F%E8%AE%A1%E5%B7%B2%E6%B5%8B%E8%AF%95%E8%AE%BE%E5%A4%87/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/138672383" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-tested-devices-after-test-operations/solutions/2772739/letmefly-2960tong-ji-yi-ce-shi-she-bei-k-59qt/" target="_blank">LeetCode题解</a>|
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
---
2+
title: 2928.给小朋友们分糖果 I
3+
date: 2024-06-01 11:52:03
4+
tags: [题解, LeetCode, 简单, 数学, 组合数学, 枚举, 模拟, 暴力]
5+
---
6+
7+
# 【LetMeFly】2928.给小朋友们分糖果 I:Java提交的运行时间超过了61%的用户
8+
9+
力扣题目链接:[https://leetcode.cn/problems/distribute-candies-among-children-i/](https://leetcode.cn/problems/distribute-candies-among-children-i/)
10+
11+
<p>给你两个正整数&nbsp;<code>n</code> 和&nbsp;<code>limit</code>&nbsp;。</p>
12+
13+
<p>请你将 <code>n</code>&nbsp;颗糖果分给 <code>3</code>&nbsp;位小朋友,确保没有任何小朋友得到超过 <code>limit</code>&nbsp;颗糖果,请你返回满足此条件下的&nbsp;<strong>总方案数</strong>&nbsp;。</p>
14+
15+
<p>&nbsp;</p>
16+
17+
<p><strong class="example">示例 1:</strong></p>
18+
19+
<pre>
20+
<b>输入:</b>n = 5, limit = 2
21+
<b>输出:</b>3
22+
<b>解释:</b>总共有 3 种方法分配 5 颗糖果,且每位小朋友的糖果数不超过 2 :(1, 2, 2) ,(2, 1, 2) 和 (2, 2, 1) 。
23+
</pre>
24+
25+
<p><strong class="example">示例 2:</strong></p>
26+
27+
<pre>
28+
<b>输入:</b>n = 3, limit = 3
29+
<b>输出:</b>10
30+
<b>解释:</b>总共有 10 种方法分配 3 颗糖果,且每位小朋友的糖果数不超过 3 :(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) 和 (3, 0, 0) 。
31+
</pre>
32+
33+
<p>&nbsp;</p>
34+
35+
<p><strong>提示:</strong></p>
36+
37+
<ul>
38+
<li><code>1 &lt;= n &lt;= 50</code></li>
39+
<li><code>1 &lt;= limit &lt;= 50</code></li>
40+
</ul>
41+
42+
43+
44+
## 解题方法:模拟
45+
46+
用$x$从$0$到$\min(limit, n)$模拟第一个小朋友,用$y$从$0$到$\min(limit, n-x)$模拟第二个小朋友,则第三个小朋友能分到$n-x-y$个。如果$n-x-y\leq limit$,则视为一种可行方案。
47+
48+
+ 时间复杂度$O(n^2)$
49+
+ 空间复杂度$O(1)$
50+
51+
### AC代码
52+
53+
#### C++
54+
55+
```cpp
56+
class Solution {
57+
public:
58+
int distributeCandies(int n, int limit) {
59+
int ans = 0;
60+
for (int x = 0; x <= n && x <= limit; x++) {
61+
for (int y = 0; y <= n - x && y <= limit; y++) {
62+
if (n - x - y <= limit) {
63+
ans++;
64+
}
65+
}
66+
}
67+
return ans;
68+
}
69+
};
70+
```
71+
72+
#### Go
73+
74+
```go
75+
// package main
76+
77+
func distributeCandies(n int, limit int) int {
78+
ans := 0
79+
for x := 0; x <= n && x <= limit; x++ {
80+
for y := 0; y <= n - x && y <= limit; y++ {
81+
if n - x - y <= limit {
82+
ans++
83+
}
84+
}
85+
}
86+
return ans
87+
}
88+
```
89+
90+
#### Java
91+
92+
```java
93+
class Solution {
94+
public int distributeCandies(int n, int limit) {
95+
int ans = 0;
96+
for (int x = 0; x <= n && x <= limit; x++) {
97+
for (int y = 0; y <= n - x && y <= limit; y++) {
98+
if (n - x - y <= limit) {
99+
ans++;
100+
}
101+
}
102+
}
103+
return ans;
104+
}
105+
}
106+
```
107+
108+
+ 执行用时分布```1 ms```,击败**61**.78%使用```Java```的用户;
109+
+ 消耗内存分布```40.03 MB```,击败```5.10%```使用```Java```的用户。
110+
111+
#### Python
112+
113+
```python
114+
class Solution:
115+
def distributeCandies(self, n: int, limit: int) -> int:
116+
ans = 0
117+
for x in range(min(limit, n) + 1):
118+
for y in range(min(n - x, limit) + 1):
119+
if n - x - y <= limit:
120+
ans += 1
121+
return ans
122+
```
123+
124+
![61快乐](https://cdn.letmefly.xyz/img/img/happy61_2024.jpg)
125+
126+
> 同步发文于CSDN和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2024/06/01/LeetCode%202928.%E7%BB%99%E5%B0%8F%E6%9C%8B%E5%8F%8B%E4%BB%AC%E5%88%86%E7%B3%96%E6%9E%9CI/)哦~
127+
>
128+
> Tisfy:[https://letmefly.blog.csdn.net/article/details/139380754](https://letmefly.blog.csdn.net/article/details/139380754)

0 commit comments

Comments
 (0)