코린이의 소소한 공부노트

[LeetCode/Easy] 575. Distribute Candies 본문

코딩테스트 풀이/JAVA

[LeetCode/Easy] 575. Distribute Candies

무지맘 2023. 3. 6. 16:42

1. Input

1) int[] candyType

- candyType[i]i번째 사탕의 타입을 나타낸다.

 

2. Output

1) n개의 사탕 중 절반만 먹을 수 있다고 할 때, 먹을 수 있는 사탕의 종류 수 중 가장 큰 것을 반환

 

3. Constraint

1) n == candyType.length

2) 2 <= n <= 10^4

3) n은 항상 짝수이다.

4) - 10^5 <= candyType[i] <= 10^5

 

4. Example

Input: candyType = [1,1,2,3] -> Output: 2

설명:

- 사탕은 4/2 = 2개 먹을 수 있고, 3종류가 있으므로 (1,2), (1,3), (2,3)을 먹을 수 있다. 따라서 먹을 수 있는 사탕의 종류는 최대 2종류이다.

 

5. Code

1) 첫 코드(2023/03/06)

HashSet<Integer> s = new HashSet<Integer>();
for(int i : candyType)
    s.add(i);
return s.size()>candyType.length/2 ? candyType.length/2 : s.size();