Skip to content

Commit ae8f4de

Browse files
committed
update: 添加问题“575.分糖果”的代码和题解
1 parent 5e2100f commit ae8f4de

7 files changed

+203
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-06-02 09:38:52
4+
* @LastEditors: LetMeFly
5+
* @LastEditTime: 2024-06-02 09:40:13
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int distributeCandies(vector<int>& candyType) {
14+
set<int> se(candyType.begin(), candyType.end());
15+
return min(se.size(), candyType.size() / 2);
16+
}
17+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-06-02 09:45:36
4+
* @LastEditors: LetMeFly
5+
* @LastEditTime: 2024-06-02 09:57:46
6+
*/
7+
package main
8+
9+
func min(a int, b int) int {
10+
if a <= b {
11+
return a
12+
}
13+
return b
14+
}
15+
16+
func distributeCandies(candyType []int) int {
17+
se := make(map[int]int)
18+
for _, t := range candyType {
19+
se[t] = 0
20+
}
21+
return min(len(se), len(candyType) / 2)
22+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-06-02 09:41:57
4+
* @LastEditors: LetMeFly
5+
* @LastEditTime: 2024-06-02 09:44:00
6+
*/
7+
import java.util.HashSet;
8+
import java.util.Set;
9+
10+
class Solution {
11+
public int distributeCandies(int[] candyType) {
12+
Set<Integer> se = new HashSet<>();
13+
for (int t : candyType) {
14+
se.add(t);
15+
}
16+
return Math.min(se.size(), candyType.length / 2);
17+
}
18+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2024-06-02 09:40:48
4+
LastEditors: LetMeFly
5+
LastEditTime: 2024-06-02 09:41:08
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def distributeCandies(self, candyType: List[int]) -> int:
11+
return min(len(set(candyType)), len(candyType) // 2)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@
211211
|0564.寻找最近的回文数|困难|<a href="https://leetcode.cn/problems/find-the-closest-palindrome/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/03/03/LeetCode%200564.%E5%AF%BB%E6%89%BE%E6%9C%80%E8%BF%91%E7%9A%84%E5%9B%9E%E6%96%87%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/123254172" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-closest-palindrome/solution/letmefly-da-an-hou-xuan-by-letmefly-2-k2vn/" target="_blank">LeetCode题解</a>|
212212
|0565.数组嵌套|中等|<a href="https://leetcode.cn/problems/array-nesting/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/07/17/LeetCode%200565.%E6%95%B0%E7%BB%84%E5%B5%8C%E5%A5%97/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/125828684" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/array-nesting/solution/by-tisfy-qior/" target="_blank">LeetCode题解</a>|
213213
|0567.字符串的排列|中等|<a href="https://leetcode.cn/problems/permutation-in-string/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/03/18/LeetCode%200567.%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E6%8E%92%E5%88%97/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/129636871" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/permutation-in-string/solutions/2176236/letmefly-567zi-fu-chuan-de-pai-lie-by-ti-dmer/" target="_blank">LeetCode题解</a>|
214+
|0575.分糖果|简单|<a href="https://leetcode.cn/problems/distribute-candies/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/06/02/LeetCode%200575.%E5%88%86%E7%B3%96%E6%9E%9C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139387726" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/distribute-candies/solutions/2798047/letmefly-575fen-tang-guo-mintype-size2-b-e9ub/" target="_blank">LeetCode题解</a>|
214215
|0589.N叉树的前序遍历|简单|<a href="https://leetcode.cn/problems/n-ary-tree-preorder-traversal/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/02/18/LeetCode%200589.N%E5%8F%89%E6%A0%91%E7%9A%84%E5%89%8D%E5%BA%8F%E9%81%8D%E5%8E%86/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/136149332" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/n-ary-tree-preorder-traversal/solutions/2643982/letmefly-589n-cha-shu-de-qian-xu-bian-li-eip2/" target="_blank">LeetCode题解</a>|
215216
|0590.N叉树的后序遍历|简单|<a href="https://leetcode.cn/problems/n-ary-tree-postorder-traversal/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/02/19/LeetCode%200590.N%E5%8F%89%E6%A0%91%E7%9A%84%E5%90%8E%E5%BA%8F%E9%81%8D%E5%8E%86/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/136167758" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/n-ary-tree-postorder-traversal/solutions/2645454/letmefly-590n-cha-shu-de-hou-xu-bian-li-nqpsr/" target="_blank">LeetCode题解</a>|
216217
|0592.分数加减运算|中等|<a href="https://leetcode.cn/problems/fraction-addition-and-subtraction/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/07/27/LeetCode%200592.%E5%88%86%E6%95%B0%E5%8A%A0%E5%87%8F%E8%BF%90%E7%AE%97/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/126011320" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/fraction-addition-and-subtraction/solution/letmefly-592fen-shu-jia-jian-yun-suan-sh-j5qw/" target="_blank">LeetCode题解</a>|
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
---
2+
title: 575.分糖果
3+
date: 2024-06-02 09:58:56
4+
tags: [题解, LeetCode, 简单, 数组, 哈希表]
5+
---
6+
7+
# 【LetMeFly】575.分糖果:min(type, size/2)
8+
9+
力扣题目链接:[https://leetcode.cn/problems/distribute-candies/](https://leetcode.cn/problems/distribute-candies/)
10+
11+
<p>Alice 有 <code>n</code> 枚糖,其中第 <code>i</code> 枚糖的类型为 <code>candyType[i]</code> 。Alice 注意到她的体重正在增长,所以前去拜访了一位医生。</p>
12+
13+
<p>医生建议 Alice 要少摄入糖分,只吃掉她所有糖的 <code>n / 2</code> 即可(<code>n</code> 是一个偶数)。Alice 非常喜欢这些糖,她想要在遵循医生建议的情况下,尽可能吃到最多不同种类的糖。</p>
14+
15+
<p>给你一个长度为 <code>n</code> 的整数数组 <code>candyType</code> ,返回: Alice <em>在仅吃掉 <code>n / 2</code> 枚糖的情况下,可以吃到糖的 <strong>最多</strong> 种类数</em>。</p>
16+
17+
<p>&nbsp;</p>
18+
19+
<p><strong>示例 1:</strong></p>
20+
21+
<pre>
22+
<strong>输入:</strong>candyType = [1,1,2,2,3,3]
23+
<strong>输出:</strong>3
24+
<strong>解释:</strong>Alice 只能吃 6 / 2 = 3 枚糖,由于只有 3 种糖,她可以每种吃一枚。
25+
</pre>
26+
27+
<p><strong>示例 2:</strong></p>
28+
29+
<pre>
30+
<strong>输入:</strong>candyType = [1,1,2,3]
31+
<strong>输出:</strong>2
32+
<strong>解释:</strong>Alice 只能吃 4 / 2 = 2 枚糖,不管她选择吃的种类是 [1,2]、[1,3] 还是 [2,3],她只能吃到两种不同类的糖。
33+
</pre>
34+
35+
<p><strong>示例 3:</strong></p>
36+
37+
<pre>
38+
<strong>输入:</strong>candyType = [6,6,6,6]
39+
<strong>输出:</strong>1
40+
<strong>解释:</strong>Alice 只能吃 4 / 2 = 2 枚糖,尽管她能吃 2 枚,但只能吃到 1 种糖。
41+
</pre>
42+
43+
<p>&nbsp;</p>
44+
45+
<p><strong>提示:</strong></p>
46+
47+
<ul>
48+
<li><code>n == candyType.length</code></li>
49+
<li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li>
50+
<li><code>n</code> 是一个偶数</li>
51+
<li><code>-10<sup>5</sup> &lt;= candyType[i] &lt;= 10<sup>5</sup></code></li>
52+
</ul>
53+
54+
55+
56+
## 解题方法:比较
57+
58+
限制Alice能吃到糖的种类的因素有两个:
59+
60+
1. 糖本身的种类——无论Alice使用什么策略都无法突破糖原本种类数的限制;
61+
2. 糖的总个数——医生让她最多吃一半数量的糖。
62+
63+
因此最终答案为$\min(type, \frac{size}2)$
64+
65+
+ 时间复杂度$O(size)$
66+
+ 空间复杂度$O(size)$
67+
68+
### AC代码
69+
70+
#### C++
71+
72+
```cpp
73+
class Solution {
74+
public:
75+
int distributeCandies(vector<int>& candyType) {
76+
set<int> se(candyType.begin(), candyType.end());
77+
return min(se.size(), candyType.size() / 2);
78+
}
79+
};
80+
```
81+
82+
#### Go
83+
84+
```go
85+
package main
86+
87+
func min(a int, b int) int {
88+
if a <= b {
89+
return a
90+
}
91+
return b
92+
}
93+
94+
func distributeCandies(candyType []int) int {
95+
se := make(map[int]int)
96+
for _, t := range candyType {
97+
se[t] = 0
98+
}
99+
return min(len(se), len(candyType) / 2)
100+
}
101+
```
102+
103+
#### Java
104+
105+
```java
106+
// import java.util.HashSet;
107+
// import java.util.Set;
108+
109+
class Solution {
110+
public int distributeCandies(int[] candyType) {
111+
Set<Integer> se = new HashSet<>();
112+
for (int t : candyType) {
113+
se.add(t);
114+
}
115+
return Math.min(se.size(), candyType.length / 2);
116+
}
117+
}
118+
```
119+
120+
#### Python
121+
122+
```python
123+
# from typing import List
124+
125+
class Solution:
126+
def distributeCandies(self, candyType: List[int]) -> int:
127+
return min(len(set(candyType)), len(candyType) // 2)
128+
```
129+
130+
> 同步发文于CSDN和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2024/06/02/LeetCode%200575.%E5%88%86%E7%B3%96%E6%9E%9C/)哦~
131+
>
132+
> Tisfy:[https://letmefly.blog.csdn.net/article/details/139387726](https://letmefly.blog.csdn.net/article/details/139387726)

Solutions/Other-Japanese-LearningNotes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ xx型
138138
|飲み(のみ)||
139139
|食べ(たべ)||
140140
|遊び(あそび)||
141+
|泳ぎ(およぎ)|游泳|
141142
|見(み)||
142143
|書き(かき)||
143144
|買い(かい)||
@@ -254,6 +255,7 @@ xx型
254255
|にわ|院子|
255256
|トイレ|厕所|
256257
|がっこう|学校|
258+
|公園(こうえん)|公园|
257259
|へや|部屋|
258260
|家(いえ)||
259261
|ぎんこう|银行|

0 commit comments

Comments
 (0)