DFS
https://school.programmers.co.kr/learn/courses/30/lessons/131130
import java.util.*;
class Solution {
static List<Integer> list = new ArrayList<>();
static boolean[] visited = new boolean[100];
public int solution(int[] cards) {
int answer = 0;
for(int i=0; i<cards.length; i++) {
int boxCnt = 0;
int idx = i;
while(!visited[idx]) {
visited[idx] = true;
boxCnt++;
idx = cards[idx] - 1;
}
list.add(boxCnt);
}
Collections.sort(list, Collections.reverseOrder());
return list.get(0) * list.get(1);
}
}
for(int i=0; i<cards.length; i++) {
int boxCnt = 0;
int idx = i;
while(!visited[idx]) {
visited[idx] = true;
boxCnt++;
idx = cards[idx] - 1;
}
list.add(boxCnt);
}
20분