forked from GreatAlgorithm-Study/AlgorithmStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJW_132265.java
More file actions
24 lines (23 loc) · 1.09 KB
/
JW_132265.java
File metadata and controls
24 lines (23 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.HashSet;
class JW_132265 {
public int solution(int[] topping) {
int answer = 0;
int len = topping.length;
// 종류 수를 계산하기 위한 Set
HashSet<Integer> setA = new HashSet<>();
HashSet<Integer> setB = new HashSet<>();
int[] typeA = new int[len]; // 아래에서 위로 토핑의 수를 저장할 배열
int[] typeB = new int[len]; // 위에서 아래로 토핑의 수를 저장할 배열
for (int i = 0; i < len; i++) {
setA.add(topping[i]); // 종류 추가
setB.add(topping[len - 1 - i]); // Set이기에 중복을 허용하지 않음
typeA[i] = setA.size(); // i번째까지의 종류 수를 저장
typeB[len - 1 - i] = setB.size(); // 끝에서 i번째까지의 종류 수를 저장
}
for (int i = 0; i < len - 1; i++)
// i와 i + 1의 사이를 잘랐는데 종류수가 같다면
if (typeA[i] == typeB[i + 1])
answer++;
return answer;
}
}