본문 바로가기
알고리즘 & 자료구조/프로그래머스

혼자 놀기의 달인

by 신재권 2023. 3. 27.
package programmers;

import java.util.PriorityQueue;

public class 혼자_놀기의_달인 {

   //2 <= cards.length <= 100
   //cards[i] = i + 1번 상자에 담긴 카드에 적힌 숫자
   //cards[i] = x -> cards[x] = y -> cards[y] = i : 열려 있는 상자 까지 계속 연다. -> 1번 그룹
   //1번 그룹을 제외하고 남는 상자가 없으면 게임 종료 -> 점수 0점 획득
   //남은 상자 중 임의의 상자를 골라서 똑같이 그룹을 만듬
   //1번 그룹 x ... x n 번 그룹 = 최대 값 구하기

   private static boolean[] visited;
   private static int[] card;
   private static PriorityQueue<Integer> pq;

   public static int solution(int[] cards) {
      card = cards;
      visited = new boolean[cards.length];
      pq = new PriorityQueue<>((o1, o2) -> o2 - o1);

      for (int i = 0; i < cards.length; i++) {
         if (!visited[i]) {
            int count = go(i, 0);
            pq.add(count);
         }
      }

      return pq.size() < 2 ? 0 : pq.poll() * pq.poll();
   }

   private static int go(int current, int count) {
      if (!visited[current]) {
         int x = card[current];
         visited[current] = true;
         count = go(x - 1, count + 1);
      }
      return count;
   }

   public static void main(String[] args) {
      System.out.println(solution(new int[] {8, 6, 3, 7, 2, 5, 1, 4}) == 12);
      System.out.println(solution(new int[] {2, 3, 1}) == 0);
   }
}

'알고리즘 & 자료구조 > 프로그래머스' 카테고리의 다른 글

N Queen  (0) 2023.03.29
테이블 해시 함수  (0) 2023.03.28
마법의 엘레베이터  (0) 2023.03.25
숫자 카드 나누기  (0) 2023.03.23
호텔 대실  (0) 2023.03.22