• 문제 링크
Distribute Candies - LeetCode
Can you solve this real interview question? Distribute Candies - 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. The doctor advised Alice to only eat n / 2 of the can
leetcode.com
• 풀이 코드
import java.util.HashSet;
import java.util.Set;
public class Solution {
public int distributeCandies(int[] candyType) {
Set<Integer> set = new HashSet<>();
for (int i : candyType) set.add(i);
return Math.min(set.size(), candyType.length / 2);
}
}'Problem Solving > LeetCode' 카테고리의 다른 글
| [LeetCode] 590. N-ary Tree Postorder Traversal - Java (0) | 2026.09.07 |
|---|---|
| [LeetCode] 589. N-ary Tree Preorder Traversal - Java (0) | 2026.09.06 |
| [LeetCode] 572. Subtree of Another Tree - Java (0) | 2026.09.04 |
| [LeetCode] 566. Reshape the Matrix - Java (0) | 2026.09.03 |
| [LeetCode] 563. Binary Tree Tilt - Java (0) | 2026.09.02 |
댓글