LeetCode 575. Distribute Candies in Java

In this post we will solve the LeetCode 575. Distribute Candies problem.

This problem is not too difficult to get to an answer. Not sure if it was network traffic, but the different attempts, which were accepted, required a different approach. Once you get a first pass accepted, I would suggest that before you leave the LeetCode website, take a look at the implementations that perform best. I was going to implement some of those approaches but got late and had to move on with other tasks.

Alice has n candies, where the ith candy is of type candyType[i]. 
Alice noticed that she started to gain weight, so she visited a doctor.

The doctor advised Alice to only eat n / 2 of the candies she has (n is always even). 
Alice likes her candies very much, 
and she wants to eat the maximum number of different types of candies while still following the doctor's advice.

Given the integer array candyType of length n, 
return the maximum number of different types of candies she can eat if she only eats n / 2 of them.


o n == candyType.length
o 2 <= n <= 10^4
o n is even.
o -10^5 <= candyType[i] <= 10^5

In this problem Alice is given different candies. As we will see in the examples, a type of candy may appear once or more times. Continue reading “LeetCode 575. Distribute Candies in Java”