Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BOJ] 21315_카드 섞기 / 골드5 / 30분 / X #358

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

bono039
Copy link
Owner

@bono039 bono039 commented May 28, 2024

📖 문제


💡 풀이 방식

브루프토스

  1. 배열 크기 N과 배열 요소들을 입력받는다.
  2. 2중 for문을 사용해 리스트를 섞는 횟수를 결정하고(완전탐색), 각 경우에 대해 리스트를 섞어본다.
  3. 섞인 리스트가 1에서 입력받은 배열과 일치한다면, 2개의 리스트 섞는 횟수를 반환하고 종료한다.

  • 리스트가 주어진 배열과 같은지 확인하는 함수

    private static boolean chk() {
        for(int i = 0 ; i < N ; i++) {
            if(arr[i] != list.get(i))
                return false;
        }
        return true;
    }
    
  • 리스트 섞는 함수

    private static void shuffle(int k) {
      int x = 1;	// 시작 인덱스
          
      // k번 반복
      while(k - x + 1 >= 0) {
        int cnt = (int)Math.pow(2, k-x+1);	// 2의 k-x+1 승
              
        // cnt번 반복
        for(int j = 0 ; j < cnt ; j++) {
            if(x == 1)	// 리스트의 마지막 요소를 첫 번째로 이동          
                list.addFirst(list.removeLast());
            else	// 2의 k-x+2 승 - 1 위치의 요소를 첫 번째로 이동                            
               list.addFirst(list.remove((int)Math.pow(2, k-x+2) -1));
        }
        
        x++;	// 인덱스 증가
      }
    }
    

🤔 어려웠던 점

  • 바뀐 배열에서 원본 배열로 거꾸로 바꿔보면서 정답을 구할 생각을 했었다,,,,,,
  • 문제 자체를 잘못 이해했다
  • 섞는 과정에서 인덱스가 헷갈렸다,,

❗ 새로 알게 된 내용

  • 2개만 뽑으면 되는 거니까 완전탐색으로 진행해서 찾아도 된다!
  • ArrayList vs LinkedList 차이점

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant