HashSet, beats 90%, explained


  • 0
    N

    Get the number of distinct candies by Set. If the number of rest candies is smaller that half, than, need to give bro some distinct candies

    public class Solution {
        public int distributeCandies(int[] candies) {
            HashSet<Integer> set = new HashSet<Integer>();
            for (int i = 0; i < candies.length; i++) {
                set.add(candies[i]); 
            }
            return Math.min(set.size(), (candies.length / 2 + candies.length % 2));
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.