Skip to content

Commit 7aacd93

Browse files
solves distribute candies
1 parent ccd8c3f commit 7aacd93

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@
151151
| 563 | [Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt) | Easy | [![Java](assets/java.png)](src/BinaryTreeTilt.java) [![Python](assets/python.png)](python/binary_tree_tilt.py) |
152152
| 566 | [Reshape The Matrix](https://leetcode.com/problems/reshape-the-matrix) | Easy | [![Java](assets/java.png)](src/ReshapeTheMatrix.java) [![Python](assets/python.png)](python/reshape_the_matrix.py) |
153153
| 572 | [Subtree of Another Tree](https://leetcode.com/problems/subtree-of-another-tree) | Easy | [![Java](assets/java.png)](src/SubtreeOfAnotherTree.java) [![Python](assets/python.png)](python/subtree_of_another_tree.py) |
154-
| 575 | [Distribute Candies](https://leetcode.com/problems/distribute-candies) | Easy | |
154+
| 575 | [Distribute Candies](https://leetcode.com/problems/distribute-candies) | Easy | [![Java](assets/java.png)](src/DistributeCandies.java) [![Python](assets/python.png)](python/distribute_candies.py) |
155155
| 581 | [Shortest Unsorted Continuous Subarray](https://leetcode.com/problems/shortest-unsorted-continuous-subarray) | Easy | |
156156
| 589 | [N-Ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal) | Easy | |
157157
| 590 | [N-Ary Tree Postorder Traversal](https://leetcode.com/problems/n-ary-tree-postorder-traversal) | Easy | |

Diff for: python/distribute_candies.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def distributeCandies(self, candyType: List[int]) -> int:
6+
return min(len(candyType) // 2, len(set(candyType)))

Diff for: src/DistributeCandies.java

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import java.util.Arrays;
2+
import java.util.HashSet;
3+
import java.util.Set;
4+
5+
public class DistributeCandies {
6+
public int distributeCandies(int[] candyType) {
7+
Set<Integer> candyTypes = new HashSet<>();
8+
for (int type : candyType) {
9+
candyTypes.add(type);
10+
}
11+
return Math.min(candyType.length / 2, candyTypes.size());
12+
}
13+
}

0 commit comments

Comments
 (0)