231009 혼자 놀기의 달인

Jongleee·2023년 10월 9일
0

TIL

목록 보기
385/737
public int solution(int[] cards) {
	int[] maxCircuitSizes = { 0, 0 };

	for (int i = 0; i < cards.length; i++) {
		if (cards[i] == -1) {
			continue;
		}
		int currentCircuitSize = calculateCircuitSize(cards, i);
		if (currentCircuitSize > maxCircuitSizes[0]) {
			maxCircuitSizes[1] = maxCircuitSizes[0];
			maxCircuitSizes[0] = currentCircuitSize;
		} else if (currentCircuitSize > maxCircuitSizes[1]) {
			maxCircuitSizes[1] = currentCircuitSize;
		}
		if (maxCircuitSizes[0] == cards.length) {
			return 0;
		}
	}

	return maxCircuitSizes[0] * maxCircuitSizes[1];
}

private int calculateCircuitSize(int[] cards, int index) {
	if (cards[index] == -1) {
		return 0;
	}
	int nextIndex = cards[index] - 1;
	cards[index] = -1;
	return calculateCircuitSize(cards, nextIndex) + 1;
}

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

0개의 댓글