Skip to content

Commit 5f98de9

Browse files
committed
solve problem Distribute Candies
1 parent 3cf3180 commit 5f98de9

File tree

5 files changed

+31
-0
lines changed

5 files changed

+31
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ All solutions will be accepted!
6767
|669|[Trim A Binary Search Tree](https://leetcode-cn.com/problems/trim-a-binary-search-tree/description/)|[java/py/js](./algorithms/TrimABinarySearchTree)|Easy|
6868
|806|[Number Of Lines To Write String](https://leetcode-cn.com/problems/number-of-lines-to-write-string/description/)|[java/py/js](./algorithms/NumberOfLinesToWriteString)|Easy|
6969
|566|[Reshape The Matrix](https://leetcode-cn.com/problems/reshape-the-matrix/description/)|[java/py/js](./algorithms/ReshapeTheMatrix)|Easy|
70+
|575|[Distribute Candies](https://leetcode-cn.com/problems/distribute-candies/description/)|[java/py/js](./algorithms/DistributeCandies)|Easy|
7071

7172
# Database
7273
|#|Title|Solution|Difficulty|
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Distribute Candies
2+
This problem is easy to solve by set
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public int distributeCandies(int[] candies) {
3+
Set<Integer> candyKinds = new HashSet<Integer>();
4+
for (int candy : candies) {
5+
candyKinds.add(candy);
6+
}
7+
return candyKinds.size() > candies.length / 2 ? candies.length / 2 : candyKinds.size();
8+
}
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* @param {number[]} candies
3+
* @return {number}
4+
*/
5+
var distributeCandies = function(candies) {
6+
let candyKinds = new Set(candies)
7+
return candyKinds.size > candies.length / 2 ? candies.length / 2 : candyKinds.size
8+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution(object):
2+
def distributeCandies(self, candies):
3+
"""
4+
:type candies: List[int]
5+
:rtype: int
6+
"""
7+
candy_kinds = set(candies)
8+
if len(candy_kinds) > len(candies) / 2:
9+
return len(candies) / 2
10+
else:
11+
return len(candy_kinds)

0 commit comments

Comments
 (0)