Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9bdfdc7

Browse files
feat: add solutions to lc problem: No.0575 (doocs#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‎

Lines changed: 15 additions & 3 deletions
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‎

Lines changed: 15 additions & 3 deletions
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 -->
Lines changed: 1 addition & 2 deletions
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
};
Lines changed: 4 additions & 0 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /