본문 바로가기
Problem Solving/LeetCode

[LeetCode] 575. Distribute Candies - Java

by graycode 2026. 9. 5.

• 문제 링크

 

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);
    }

}

댓글