[LeetCode] Distribute Candies

Given an integer array with even length, where different numbers in this array represent different kinds of candies. Each number means one candy of the corresponding kind. You need to distribute these candies equally in number to brother and sister. Return the maximum number of kinds of candies the sister could gain.

Example 1:

Input: candies = [1,1,2,2,3,3]
Output: 3
Explanation:
There are three different kinds of candies (1, 2 and 3), and two candies for each kind.
Optimal distribution: The sister has candies [1,2,3] and the brother has candies [1,2,3], too.
The sister has three different kinds of candies. 

Example 2:

Input: candies = [1,1,2,3]
Output: 2
Explanation: For example, the sister has candies [2,3] and the brother has candies [1,1].
The sister has two different kinds of candies, the brother has only one kind of candies. 

Note:

  1. The length of the given array is in range [2, 10,000], and will be even.
  2. The number in given array is in range [-100,000, 100,000].

题目要求把偶数个种类不同的糖果分给哥哥和妹妹两个人,要求妹妹分得的糖果种类最多。因为有2n个糖果,所以妹妹分的的糖果种类数最多为n / 2,根据set的特性将糖果的种类m计算出来。如果m > n / 2,则取 n / 2,如果m < n / 2,则取m。

class Solution {
public:
    int distributeCandies(vector<int>& candies) {
        unordered_set<int> kind;
        for (int candy : candies)
            kind.insert(candy);
        return min(kind.size(), candies.size() / 2);
    }
};
// 309 ms

用for循环计算糖果的种类,并根据for循环的判断条件得出妹妹分得糖果种类的最大值。

class Solution {
public:
    int distributeCandies(vector<int>& candies) {
        sort(candies.begin(), candies.end());
        int kind = 1;
        for (int i = 1; i != candies.size() && kind != candies.size() / 2; i++)
            if (candies[i] != candies[i - 1])
                kind++;
        return kind;
    }
};
// 276 ms
时间: 2024-08-28 14:55:32

[LeetCode] Distribute Candies的相关文章

[LeetCode] Distribute Candies 分糖果

Given an integer array with even length, where different numbers in this array represent different kinds of candies. Each number means one candy of the corresponding kind. You need to distribute these candies equally in number to brother and sister.

LeetCode解题思路:575. Distribute Candies

Given an integer array with even length, where different numbers in this array represent different kinds of candies. Each number means one candy of the corresponding kind. You need to distribute these candies equally in number to brother and sister.

LeetCode 575. Distribute Candies (发糖果)

Given an integer array with even length, where different numbers in this array represent different kinds of candies. Each number means one candy of the corresponding kind. You need to distribute these candies equally in number to brother and sister.

LeetCode - 575. Distribute Candies

Given an integer array with even length, where different numbers in this array represent different kinds of candies. Each number means one candy of the corresponding kind. You need to distribute these candies equally in number to brother and sister.

LeetCode算法题-Distribute Candies(Java实现)

这是悦乐书的第266次更新,第279篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第133题(顺位题号是575).给定具有偶数长度的整数数组,其中该数组中的不同数字表示不同种类的糖果. 每个数字表示相应种类的一种糖果. 您需要将这些糖果平均分配给哥哥妹妹. 返回妹妹可以获得的最多种类数量的糖果.例如: 输入:糖果= [1,1,2,2,3,3] 输出:3 说明:有三种不同的糖果(1,2和3),每种糖果两种.最佳分配:妹妹有糖果[1,2,3],哥哥也有糖果[1,2,3]

LeetCode.1103-向人们分发糖果(Distribute Candies to People)

这是小川的第393次更新,第425篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第256题(顺位题号是1103).我们通过以下方式向一排n = num_people个人分发一些糖果: 给第一个人送1个糖果,给第二个人送2个糖果,依此类推,直到我们给最后一个人送糖果.然后,我们回到行的开头,向第一个人提供n + 1个糖果,向第二个人提供n + 2个糖果,依此类推,直到我们向最后一个人提供2 * n个糖果. 这个过程重复进行,直到我们用完糖果.最后一个人将得到所有剩余的

LeetCode 1103. Distribute Candies to People (分糖果 II)

题目标签:Math 题目让我们分发糖果,分的糖果从1 开始依次增加,直到分完. for loop可以计数糖果的数量,直到糖果发完.但是还是要遍历array 给people 发糖,这里要用到 index = (本轮分糖果的量 % people 的人数)糖果的数量从0 开始计数,这样的话,index 就会一直重复遍历 array,具体看code. Java Solution: Runtime:  1ms, faster than 90.53% Memory Usage: 33.8 MB, less

[LeetCode] 1103. Distribute Candies to People 分糖果

题目: 思路: 本题一开始的思路就是按照流程一步步分下去,算是暴力方法,在官方题解中有利用等差数列进行计算的 这里只记录一下自己的暴力解题方式 只考虑每次分配的糖果数,分配的糖果数为1,2,3,4,5,..., 依次加1 再考虑到分配的轮数,可以利用 i % num_people 来求得第i次应该分配到第几个人 最后要注意的是,如果当前糖果数小于本应该分配的糖果数,则将当前糖果全部给予,也就是要判断剩余糖果数 candies 与本该分配糖果数 i+1 的大小,谁小分配谁 代码: class So

575. 分配糖果 Distribute Candies

Given an integer array with even length, where different numbers in this array represent different kinds of candies. Each number means one candy of the corresponding kind. You need to distribute these candies equally in number to brother and sister.