首页 > 代码库 > leetcode575

leetcode575

public class Solution {
    public int DistributeCandies(int[] candies) {
        var dic = new Dictionary<int, int>();
            var len=candies.Length;
            var result = 0;
            foreach (var candy in candies)
            {
                if (!dic.ContainsKey(candy))
                {
                    dic.Add(candy, 1);
                }
                else
                {
                    dic[candy]++;
                }
            }
            if (dic.Count >= len / 2)
            {
                result = len / 2;
            }
            else
            {
                result = dic.Count;
            }
            return result;
    }
}

https://leetcode.com/problems/distribute-candies/#/description

leetcode575