Skip to content

Commit f665530

Browse files
committed
day 1
1 parent 7fc5277 commit f665530

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Distribute Candies
3+
==================
4+
5+
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.
6+
7+
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.
8+
9+
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.
10+
11+
Example 1:
12+
Input: candyType = [1,1,2,2,3,3]
13+
Output: 3
14+
Explanation: Alice can only eat 6 / 2 = 3 candies. Since there are only 3 types, she can eat one of each type.
15+
16+
Example 2:
17+
Input: candyType = [1,1,2,3]
18+
Output: 2
19+
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.
20+
21+
Example 3:
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+
Constraints:
27+
n == candyType.length
28+
2 <= n <= 104
29+
n is even.
30+
-105 <= candyType[i] <= 105
31+
32+
Hint #1
33+
To maximize the number of kinds of candies, we should try to distribute candies such that sister will gain all kinds.
34+
35+
Hint #2
36+
What is the upper limit of the number of kinds of candies sister will gain? Remember candies are to distributed equally.
37+
38+
Hint #3
39+
Which data structure is the most suitable for finding the number of kinds of candies?
40+
41+
Hint #4
42+
Will hashset solves the problem? Inserting all candies kind in the hashset and then checking its size with upper limit.
43+
*/
44+
45+
class Solution
46+
{
47+
public:
48+
int distributeCandies(vector<int> &candyType)
49+
{
50+
unordered_set<int> types(candyType.begin(), candyType.end());
51+
int n = candyType.size();
52+
return min((int)n / 2, (int)types.size());
53+
}
54+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# March 2021 LeetCoding Challenge
2+
3+
| Day | Question Links | Solutions |
4+
| :-: | :------------------------------------------------------------------------------------------------------------------------------------- | :-------------------------------------: |
5+
| 1. | [Distribute Candies](https://leetcode.com/explore/challenge/card/march-leetcoding-challenge-2021/588/week-1-march-1st-march-7th/3657/) | [cpp](./01.%20Distribute%20Candies.cpp) |

Leetcode Daily Challenge/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88

99
### 2021
1010
- [January](./January-2021)
11-
- [February](./February-2021)
11+
- [February](./February-2021)
12+
- [March](./March-2021)

0 commit comments

Comments
 (0)