seanpgallivan

Posted on

Solution: Distribute Candies

This is part of a series of Leetcode solution explanations (index). If you liked this solution or found it useful, please like this post and/or upvote my solution post on Leetcode's forums.

Description:

(Jump to: Solution Idea || Code: JavaScript | Python | Java | C++)

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.

Examples:

Example 1:
Input: candyType = [1,1,2,2,3,3]
Output: 3
Explanation: Alice can only eat 6 / 2 = 3 candies.
Since there are only 3 types,
she can eat one of each type.
Example 2:
Input: candyType = [1,1,2,3]
Output: 2
Explanation: Alice can only eat 4 / 2 = 2 candies.
Whether she eats types [1,2], [1,3], or [2,3],
she still can only eat 2 different types.
Example 3:
Input: candyType = [6,6,6,6]
Output: 1
Explanation: Alice can only eat 4 / 2 = 2 candies.
Even though she can eat 2 candies,
she only has 1 type.

Constraints:

• `n == candyType.length`
• `1 <= n <= 10^4`
• `n` is even.
• `-10^5 <= candyType[i] <= 10^5`

Idea:

(Jump to: Problem Description || Code: JavaScript | Python | Java | C++)

In order to solve this problem, we need to identify how many unique types of candy there are. The easiest method to find unique values is with a set. If we convert our input array of candy types (C) to a set, then it will only contain unique values, and thus the size of the set will represent the number of different candy types.

The only other thing to remember is that we're constrained to at most C.length / 2 pieces, per the instructions, so we need to use a min() boundary before we return our answer.

Implementation:

Java alone does not have an easy set constructor from an int array. Any such solution would have to invole boxing the primitive ints into Integers before converting to a HashSet, so it's easier just to build the HashSet iteratively via a for loop.

Javascript Code:

``````const distributeCandies = C => Math.min((new Set(C)).size, C.length / 2)
``````

Python Code:

``````class Solution:
def distributeCandies(self, C: List[int]) -> int:
return min(len(set(C)), len(C) // 2)
``````

Java Code:

``````class Solution {
public int distributeCandies(int[] C) {
Set<Integer> cset = new HashSet<>();
for (int c : C) cset.add(c)
return Math.min(cset.size(), C.length / 2);
}
}
``````

C++ Code:

``````class Solution {
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://dev.to/seanpgallivan/solution-distribute-candies-3kii", "fetch_time": 1723600633000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2024-33/segments/1722641086966.85/warc/CC-MAIN-20240813235205-20240814025205-00811.warc.gz", "warc_record_offset": 169374219, "warc_record_length": 41707, "token_count": 767, "char_count": 2822, "score": 3.671875, "int_score": 4, "crawl": "CC-MAIN-2024-33", "snapshot_type": "latest", "language": "en", "language_score": 0.9032143950462341, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00048-of-00064.parquet"}