|
| 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>给你两个正整数 <code>n</code> 和 <code>limit</code> 。</p> |
| 12 | + |
| 13 | +<p>请你将 <code>n</code> 颗糖果分给 <code>3</code> 位小朋友,确保没有任何小朋友得到超过 <code>limit</code> 颗糖果,请你返回满足此条件下的 <strong>总方案数</strong> 。</p> |
| 14 | + |
| 15 | +<p> </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> </p> |
| 34 | + |
| 35 | +<p><strong>提示:</strong></p> |
| 36 | + |
| 37 | +<ul> |
| 38 | + <li><code>1 <= n <= 50</code></li> |
| 39 | + <li><code>1 <= limit <= 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 | + |
| 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