|
| 1 | +''' |
| 2 | + Alice has n candies, where the ith candy is of type |
| 3 | + candyType[i]. Alice noticed that she started to gain |
| 4 | + weight, so she visited a doctor. |
| 5 | +
|
| 6 | + The doctor advised Alice to only eat n / 2 of the candies |
| 7 | + she has (n is always even). Alice likes her candies very |
| 8 | + much, and she wants to eat the maximum number of |
| 9 | + different types of candies while still following the |
| 10 | + doctor's advice. |
| 11 | +
|
| 12 | + Given the integer array candyType of length n, return the |
| 13 | + maximum number of different types of candies she can eat |
| 14 | + if she only eats n / 2 of them. |
| 15 | +
|
| 16 | + Example: |
| 17 | + Input: candyType = [1,1,2,2,3,3] |
| 18 | + Output: 3 |
| 19 | + Explanation: Alice can only eat 6 / 2 = 3 candies. Since |
| 20 | + there are only 3 types, she can eat one of |
| 21 | + each type. |
| 22 | +
|
| 23 | + Example: |
| 24 | + Input: candyType = [1,1,2,3] |
| 25 | + Output: 2 |
| 26 | + Explanation: Alice can only eat 4 / 2 = 2 candies. Whether |
| 27 | + she eats types [1,2], [1,3], or [2,3], she |
| 28 | + still can only eat 2 different types. |
| 29 | +
|
| 30 | + Example: |
| 31 | + Input: candyType = [6,6,6,6] |
| 32 | + Output: 1 |
| 33 | + Explanation: Alice can only eat 4 / 2 = 2 candies. Even |
| 34 | + though she can eat 2 candies, she only has |
| 35 | + 1 type. |
| 36 | +
|
| 37 | + Constraints: |
| 38 | + - n == candyType.length |
| 39 | + - 2 <= n <= 10^4 |
| 40 | + - n is even. |
| 41 | + - -10^5 <= candyType[i] <= 10^5 |
| 42 | +''' |
| 43 | +#Difficulty: Easy |
| 44 | +#206 / 206 test cases passed. |
| 45 | +#Runtime: 784 ms |
| 46 | +#Memory Usage: 16.3 MB |
| 47 | + |
| 48 | +#Runtime: 784 ms, faster than 82.02% of Python3 online submissions for Distribute Candies. |
| 49 | +#Memory Usage: 16.3 MB, less than 38.32% of Python3 online submissions for Distribute Candies. |
| 50 | + |
| 51 | +class Solution: |
| 52 | + def distributeCandies(self, candyType: List[int]) -> int: |
| 53 | + return min(len(candyType)//2, len(set(candyType))) |
0 commit comments