Created
August 22, 2023 17:57
-
-
Save youngvctr/08661fdfd663999f2295893af9d671aa to your computer and use it in GitHub Desktop.
Programmers | 혼자 놀기의 달인
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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