575. Distribute Candies Easy

@problem@discussion
#Array#Hash Table



1/**
2 * [575] Distribute Candies
3 *
4 * Alice has n candies, where the i^th candy is of type candyType[i]. Alice noticed that she started to gain weight, so she visited a doctor.
5 * 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.
6 * 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.
7 *  
8 * Example 1:
9 * 
10 * Input: candyType = [1,1,2,2,3,3]
11 * Output: 3
12 * Explanation: Alice can only eat 6 / 2 = 3 candies. Since there are only 3 types, she can eat one of each type.
13 * 
14 * Example 2:
15 * 
16 * Input: candyType = [1,1,2,3]
17 * Output: 2
18 * 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.
19 * 
20 * Example 3:
21 * 
22 * Input: candyType = [6,6,6,6]
23 * Output: 1
24 * Explanation: Alice can only eat 4 / 2 = 2 candies. Even though she can eat 2 candies, she only has 1 type.
25 * 
26 *  
27 * Constraints:
28 * 
29 * 	n == candyType.length
30 * 	2 <= n <= 10^4
31 * 	n is even.
32 * 	-10^5 <= candyType[i] <= 10^5
33 * 
34 */
35pub struct Solution {}
36
37// problem: https://leetcode.com/problems/distribute-candies/
38// discuss: https://leetcode.com/problems/distribute-candies/discuss/?currentPage=1&orderBy=most_votes&query=
39
40// submission codes start here
41
42impl Solution {
43    pub fn distribute_candies(candy_type: Vec<i32>) -> i32 {
44        0
45    }
46}
47
48// submission codes end
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn test_575() {
56    }
57}
58


Back
© 2025 bowen.ge All Rights Reserved.