Skip to content

Commit 04ef754

Browse files
committed
Distribute candies.
1 parent bc32812 commit 04ef754

File tree

5 files changed

+54
-4
lines changed

5 files changed

+54
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
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.
3+
4+
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.
5+
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+
 
9+
10+
Example 1:
11+
Input: candyType = [1,1,2,2,3,3]
12+
Output: 3
13+
Explanation: Alice can only eat 6 / 2 = 3 candies. Since there are only 3 types, she can eat one of each type.
14+
15+
Example 2:
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+
Input: candyType = [6,6,6,6]
22+
Output: 1
23+
Explanation: Alice can only eat 4 / 2 = 2 candies. Even though she can eat 2 candies, she only has 1 type.
24+
 
25+
26+
Constraints:
27+
- n == candyType.length
28+
- 2 <= n <= 104
29+
- n is even.
30+
- -105 <= candyType[i] <= 105
31+
*/
32+
class Solution {
33+
func distributeCandies(_ candyType: [Int]) -> Int {
34+
let n = candyType.count / 2
35+
let type = Set(candyType)
36+
return type.count > n ? n : type.count
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Easy/575.Distribute Candies.playground/playground.xcworkspace/contents.xcworkspacedata

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Medium/43.Multiply Strings.playground/Contents.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,16 @@ class Solution {
3939
var result = ""
4040
for (i, e) in arr.enumerated() {
4141
let n = e + flagArr[i]
42+
let s = "\(n)"
4243
if n > 9 {
43-
let s = "\(n)"
4444
flagArr[i + 1] = Int(s[s.startIndex..<s.index(s.endIndex, offsetBy: -1)])!
45-
result.append("\(n)".last!)
45+
result.append(s.last!)
4646
} else {
47-
result.append("\(n)")
47+
result.append(s)
4848
}
4949
}
50+
if result.last == "0" { result.removeLast() }
5051
result = String(result.reversed())
51-
if result.first == "0" { result.removeFirst() }
5252
return result
5353
}
5454
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
108. [Maximum Depth of N-ary Tree](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/559.Maximum%20Depth%20of%20N-ary%20Tree.playground/Contents.swift)
113113
109. [Array Partition](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/561.Array%20Partition.playground/Contents.swift)
114114
110. [Reshape the Matrix](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/566.Reshape%20the%20Matrix.playground/Contents.swift)
115+
111. [Distribute Candies](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/575.Distribute%20Candies.playground/Contents.swift)
115116

116117
#### Medium
117118

0 commit comments

Comments
 (0)