C#LeetCode刷题之#575-分糖果(Distribute Candies)

问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3798 访问。

给定一个偶数长度的数组,其中不同的数字代表着不同种类的糖果,每一个数字代表一个糖果。你需要把这些糖果平均分给一个弟弟和一个妹妹。返回妹妹可以获得的最大糖果的种类数。

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

输出: 3

解析: 一共有三种种类的糖果,每一种都有两个。

最优分配方案:妹妹获得[1,2,3],弟弟也获得[1,2,3]。这样使妹妹获得糖果的种类数最多。

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

输出: 2

解析: 妹妹获得糖果[2,3],弟弟获得糖果[1,1],妹妹有两种不同的糖果,弟弟只有一种。这样使得妹妹可以获得的糖果种类数最多。

注意:

数组的长度为[2, 10,000],并且确定为偶数。
数组中数字的大小在范围[-100,000, 100,000]内。


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.

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. 

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:

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


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3798 访问。

public class Program {

    public static void Main(string[] args) {
        var words = new int[] { 1, 1, 2, 2, 3, 3 };

        var res = DistributeCandies(words);
        Console.WriteLine(res);

        Console.ReadKey();
    }

    public static int DistributeCandies(int[] candies) {
        var res = new HashSet();
        for(var i = 0; i < candies.Length; i++) {
            if(!res.Contains(candies[i])) {
                res.Add(candies[i]);
            }
        }
        if(res.Count < candies.Length / 2) {
            return res.Count;
        }
        return candies.Length / 2;
    }

}

以上给出1种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3798 访问。

3

分析:

显而易见,以上算法的时间复杂度为: O(n)

你可能感兴趣的:(C#LeetCode刷题,C#LeetCode)