跳至主要內容

575. 分糖果


575. 分糖果

🟢   🔖  数组 哈希表  🔗 力扣open in new window LeetCodeopen in new window

题目

Alice has n candies, where the ith candy is of type candyType[i]. Alice noticed that she started to gain weight, so she visited a doctor.

The doctor advised Alice to only eat n / 2 of the candies she has (n is always even). Alice likes her candies very much, and she wants to eat the maximum number of different types of candies while still following the doctor's advice.

Given the integer array candyType of length n, return _themaximum number of different types of candies she can eat if she only eats _n / 2 of them.

Example 1:

Input: candyType = [1,1,2,2,3,3]

Output: 3

Explanation: Alice can only eat 6 / 2 = 3 candies. Since there are only 3 types, she can eat one of each type.

Example 2:

Input: candyType = [1,1,2,3]

Output: 2

Explanation: Alice can only eat 4 / 2 = 2 candies. Whether she eats types [1,2], [1,3], or [2,3], she still can only eat 2 different types.

Example 3:

Input: candyType = [6,6,6,6]

Output: 1

Explanation: Alice can only eat 4 / 2 = 2 candies. Even though she can eat 2 candies, she only has 1 type.

Constraints:

  • n == candyType.length
  • 2 <= n <= 10^4
  • n is even.
  • -10^5 <= candyType[i] <= 10^5

题目大意

Alice 有 n 枚糖,其中第 i 枚糖的类型为 candyType[i] 。Alice 注意到她的体重正在增长,所以前去拜访了一位医生。

医生建议 Alice 要少摄入糖分,只吃掉她所有糖的 n / 2 即可(n 是一个偶数)。Alice 非常喜欢这些糖,她想要在遵循医生建议的情况下,尽可能吃到最多不同种类的糖。

给你一个长度为 n 的整数数组 candyType ,返回: Alice 在仅吃掉n / 2 枚糖的情况下,可以吃到糖的 最多 种类数

示例 1:

输入: candyType = [1,1,2,2,3,3]

输出: 3

解释: Alice 只能吃 6 / 2 = 3 枚糖,由于只有 3 种糖,她可以每种吃一枚。

示例 2:

输入: candyType = [1,1,2,3]

输出: 2

解释: Alice 只能吃 4 / 2 = 2 枚糖,不管她选择吃的种类是 [1,2]、[1,3] 还是 [2,3],她只能吃到两种不同类的糖。

示例 3:

输入: candyType = [6,6,6,6]

输出: 1

解释: Alice 只能吃 4 / 2 = 2 枚糖,尽管她能吃 2 枚,但只能吃到 1 种糖。

提示:

  • n == candyType.length
  • 2 <= n <= 10^4
  • n 是一个偶数
  • -10^5 <= candyType[i] <= 10^5

解题思路

  • 使用集合(Set)对糖果数组 candyType 进行去重,得到糖果的种类数 totalType
  • 结果即为 Math.min(totalType, candyType.length / 2)
    • 若糖果种类总数 totalType 小于 candyType.length / 2,可以尝到的糖果种类即为糖果的总种类数。
    • 若糖果种类总数 totalType 大于或等于 candyType.length / 2,尝到的糖果种类为糖果总数量的一半。

复杂度分析

  • 时间复杂度O(n),其中 ncandyType 的长度,使用 Set 遍历数组进行去重。
  • 空间复杂度O(k),其中 k 为糖果种类数,使用 Set 存储糖果种类

代码

/**
 * @param {number[]} candyType
 * @return {number}
 */
var distributeCandies = function (candyType) {
	const totalType = new Set(candyType).size; // 去重计算糖果种类数
	return Math.min(totalType, candyType.length / 2); // 取种类数与糖果总数的一半的最小值
};

相关题目

题号标题题解标签难度力扣
3122使矩阵满足条件的最少操作次数数组 动态规划 矩阵🟠🀄️open in new window 🔗open in new window
3142判断矩阵是否满足条件数组 矩阵🟢🀄️open in new window 🔗open in new window