99클럽 코테 스터디 38일차 TIL

2024. 8. 29. 00:20·알고리즘 공부
import java.util.*;

class Solution {
    public int solution(int[] cards) {
        int n = cards.length;
        boolean[] visited = new boolean[n];
        List<Integer> groups = new ArrayList<>();

        for (int i = 0; i < n; i++) {
            if (!visited[i]) {
                int groupSize = getGroupSize(cards, visited, i);
                groups.add(groupSize);
            }
        }

        if (groups.size() == 1) {
            return 0;
        }

        Collections.sort(groups, Collections.reverseOrder());
        return groups.get(0) * groups.get(1);
    }

    private int getGroupSize(int[] cards, boolean[] visited, int start) {
        int size = 0;
        int current = start;

        while (!visited[current]) {
            visited[current] = true;
            size++;
            current = cards[current] - 1;
        }

        return size;
    }
}

'알고리즘 공부' 카테고리의 다른 글

99클럽 코테 스터디 40일차 TIL  (0) 2024.08.31
99클럽 코테 스터디 39일차 TIL  (0) 2024.08.29
99클럽 코테 스터디 37일차 TIL  (0) 2024.08.27
99클럽 코테 스터디 36일차 TIL  (0) 2024.08.26
99클럽 코테 스터디 34일차 TIL  (0) 2024.08.25
'알고리즘 공부' 카테고리의 다른 글
  • 99클럽 코테 스터디 40일차 TIL
  • 99클럽 코테 스터디 39일차 TIL
  • 99클럽 코테 스터디 37일차 TIL
  • 99클럽 코테 스터디 36일차 TIL
HD9504
HD9504
  • HD9504
    습관
    HD9504
  • 전체
    오늘
    어제
    • 분류 전체보기
      • python
        • 트러블슈팅
        • Numpy
        • pandas
        • Wordbook in python
      • Listen to a lecture
        • school for ai in edwith
      • 용어정리
      • 전공 복습
        • 실험계획법
        • 회귀분석
        • 베이지안
        • 일반화 선형모형
      • 자연어처리
      • 글쓰기 공부
      • 밑바닥부터 시작
      • Java
      • Spring
        • JPA
        • Version
      • Web
        • HTML, CSS
        • Javascript
        • 개념, 이론
      • 하루일과
      • 알고리즘 공부
      • 사색
      • 문제해결
      • Database
        • Redis
      • Computer Science
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    spring
    spring boot
    개발자취업
    Java
    REDIS
    Decorator
    디자인패턴
    99클럽
    코딩테스트준비
    til
    항해99
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
HD9504
99클럽 코테 스터디 38일차 TIL
상단으로

티스토리툴바