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

时间: 2024-10-13 16:47:18

leetcode575的相关文章