Skip to content

Commit 9bdfdc7

Browse files
authored
feat: add solutions to lc problem: No.0575 (#2996)
No.0575.Distribute Candies
1 parent 597a926 commit 9bdfdc7

File tree

4 files changed

+35
-8
lines changed

4 files changed

+35
-8
lines changed

solution/0500-0599/0575.Distribute Candies/README.md

+15-3
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ tags:
6666

6767
<!-- solution:start -->
6868

69-
### 方法一
69+
### 方法一:哈希表
70+
71+
我们用一个哈希表来存储糖果的种类,如果糖果的种类数小于 $n / 2$,那么 Alice 最多可以吃到的糖果种类数就是糖果的种类数;否则,Alice 最多可以吃到的糖果种类数就是 $n / 2$。
72+
73+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为糖果的数量。
7074

7175
<!-- tabs:start -->
7276

@@ -98,8 +102,7 @@ class Solution {
98102
class Solution {
99103
public:
100104
int distributeCandies(vector<int>& candyType) {
101-
unordered_set<int> s;
102-
for (int c : candyType) s.insert(c);
105+
unordered_set<int> s(candyType.begin(), candyType.end());
103106
return min(candyType.size() >> 1, s.size());
104107
}
105108
};
@@ -117,6 +120,15 @@ func distributeCandies(candyType []int) int {
117120
}
118121
```
119122

123+
#### TypeScript
124+
125+
```ts
126+
function distributeCandies(candyType: number[]): number {
127+
const s = new Set(candyType);
128+
return Math.min(s.size, candyType.length >> 1);
129+
}
130+
```
131+
120132
<!-- tabs:end -->
121133

122134
<!-- solution:end -->

solution/0500-0599/0575.Distribute Candies/README_EN.md

+15-3
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ tags:
6464

6565
<!-- solution:start -->
6666

67-
### Solution 1
67+
### Solution 1: Hash Table
68+
69+
We use a hash table to store the types of candies. If the number of candy types is less than $n / 2$, then the maximum number of candy types that Alice can eat is the number of candy types. Otherwise, the maximum number of candy types that Alice can eat is $n / 2$.
70+
71+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the number of candies.
6872

6973
<!-- tabs:start -->
7074

@@ -96,8 +100,7 @@ class Solution {
96100
class Solution {
97101
public:
98102
int distributeCandies(vector<int>& candyType) {
99-
unordered_set<int> s;
100-
for (int c : candyType) s.insert(c);
103+
unordered_set<int> s(candyType.begin(), candyType.end());
101104
return min(candyType.size() >> 1, s.size());
102105
}
103106
};
@@ -115,6 +118,15 @@ func distributeCandies(candyType []int) int {
115118
}
116119
```
117120

121+
#### TypeScript
122+
123+
```ts
124+
function distributeCandies(candyType: number[]): number {
125+
const s = new Set(candyType);
126+
return Math.min(s.size, candyType.length >> 1);
127+
}
128+
```
129+
118130
<!-- tabs:end -->
119131

120132
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
class Solution {
22
public:
33
int distributeCandies(vector<int>& candyType) {
4-
unordered_set<int> s;
5-
for (int c : candyType) s.insert(c);
4+
unordered_set<int> s(candyType.begin(), candyType.end());
65
return min(candyType.size() >> 1, s.size());
76
}
87
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function distributeCandies(candyType: number[]): number {
2+
const s = new Set(candyType);
3+
return Math.min(s.size, candyType.length >> 1);
4+
}

0 commit comments

Comments
 (0)