Easy Understanding Java Solution


  • 0
    X
    public class Solution {
        public int distributeCandies(int[] candies) {
            int count = 1;
            Arrays.sort(candies);
            int n = candies.length;
            
            for (int i = 1; i < n; i++) {
                if (candies[i] != candies[i-1]) {
                    count++;
                }
            }
            return Math.min(count, n/2);
            //return count > n / 2 ? n / 2 : count;
        }
    }
    

  • 0
    G

    Good thinking,i think your solution is better than using HastSet() in Math.


  • 0
    X

    @Gene20 Thanks very much ;)


Log in to reply
 

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