LeetCode<Day2> Distribute Candies

package Day2;

/**
 * Created by syw on 2017/12/10.
 */

/**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.
 * */

import org.junit.Test;

import java.util.HashSet;
import java.util.Set;

/**思路:如果糖果种类大于糖果数的二分之一,那么妹妹最多拿到糖果数的1/2,否则就是每种都拿到也就是糖果的种类数。
 * */
public class DistributeCandy {

    public static void main(String[] args) {
        int [] candies = {1,2,1,2,1,3};
        System.out.println(distributeCandies(candies));
    }
    public  static  int distributeCandies(int[] candies) {
        Set<Integer> set = new HashSet<Integer>();
        for ( int s : candies)
        {
            set.add(s);
        }
        return set.size()>=candies.length/2 ? candies.length/2 : set.size();
    }

}
 
 
PS:仅仅记录自己LeetCode练习过程,如有借鉴别人答案请注明谅解。

猜你喜欢

转载自blog.csdn.net/weixin_38035852/article/details/78764984
今日推荐