230906 롤케이크 자르기

Jongleee·2023년 9월 6일
0

TIL

목록 보기
357/737
public int solution(int[] topping) {
	int answer = 0;
	HashSet<Integer> set = new HashSet<>();
	int[] ToppingType1 = new int[topping.length];
	int[] ToppingType2 = new int[topping.length];

	calculateDistinctToppings(topping, set, ToppingType1);
	set.clear();
	calculateDistinctToppings(reverseArray(topping), set, ToppingType2);

	for (int i = 0; i < topping.length - 1; i++) {
		if (ToppingType1[i] == ToppingType2[topping.length - i - 2]) {
			answer++;
		}
	}

	return answer;
}

public void calculateDistinctToppings(int[] topping, HashSet<Integer> set, int[] dp) {
	for (int i = 0; i < topping.length; i++) {
		set.add(topping[i]);
		dp[i] = set.size();
	}
}

public int[] reverseArray(int[] arr) {
	int[] reversed = new int[arr.length];
	for (int i = 0; i < arr.length; i++) {
		reversed[i] = arr[arr.length - i - 1];
	}
	return reversed;
}

출처:https://school.programmers.co.kr/learn/courses/30/lessons/132265

0개의 댓글