Skip to content

Instantly share code, notes, and snippets.

@youngvctr
Created August 22, 2023 17:57
Show Gist options
  • Save youngvctr/08661fdfd663999f2295893af9d671aa to your computer and use it in GitHub Desktop.
Save youngvctr/08661fdfd663999f2295893af9d671aa to your computer and use it in GitHub Desktop.
Programmers | 혼자 놀기의 달인
import java.util.*;
class Solution {
public int solution(int[] cards) {
List<Integer> result = new ArrayList<>();
boolean[] visited = new boolean[cards.length];
for(int i=0; i<cards.length; i++){
int cnt = 0;
if(!visited[i]){
int start = cards[i];
int end = 0;
while(cards[i] != end){
end = cards[start-1];
visited[start-1] = true;
start = end;
cnt++;
}
result.add(cnt);
}
}
if(result.size() == 1) return 0;
else {
Collections.sort(result, Collections.reverseOrder());
return result.get(0) * result.get(1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment