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

posted on 2017-05-09 11:50  Sempron2800+  阅读(125)  评论(0编辑  收藏  举报