|
| 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> </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> </p> |
| 44 | + |
| 45 | +<p><strong>提示:</strong></p> |
| 46 | + |
| 47 | +<ul> |
| 48 | + <li><code>n == candyType.length</code></li> |
| 49 | + <li><code>2 <= n <= 10<sup>4</sup></code></li> |
| 50 | + <li><code>n</code> 是一个偶数</li> |
| 51 | + <li><code>-10<sup>5</sup> <= candyType[i] <= 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) |
0 commit comments