Conversation
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
문제
https://school.programmers.co.kr/learn/courses/30/lessons/12936#
풀이 후기
모든 경우의 수를 구하는 방법으로 풀었다가 시간 초과가 났었다.
다른 방법은 도저히 모르겠어서 서치를 했고, 그렇게 알게 된 풀이 방법도 도무지 이해가 안 가서 정말 오래 붙들고 있었다.
원래는 레벨 3 문제였다는데 레벨 3에 있어야 할 것 같아 ..
그렇게 내가 이해한 방식은 다음과 같다.
개요
구하고자 하는 k번째 수열의 가장 첫 원소부터 순서대로 하나씩 구할 수 있다.
n 길이의 수열에 대해 가장 첫 원소를 구하고, 그 다음부터 n-1 길이의 수열에 대해 가장 첫 원소를 구하는 방식으로 말이다.
다음 규칙을 활용한다.
해당 규칙이 적용되는 이유는 다음과 같다.
따라서 특정 원소는 (n-1)!개만큼 반복되는 것이다.
위 규칙을 활용해서 풀이하는 방법은 다음과 같다.
0번에서 말했듯이, n 길이의 수열의 첫 원소를 구하고, 나머지 n-1 길이의 부분 수열의 첫 원소를 구하는 방식을 반복한다.
따라서 수열의 길이(i)를 n에서부터 1개씩 줄여나가는 반복문을 실행한다.
반복문 내부에서는
sectionSize = factorial(i - 1);sectionIndex = Math.ceil(k / sectionSize);k % sectionSize로 알 수 있다. 그렇기 때문에 다음 반복문을 위해 k를 해당 값으로 갱신해준다. 이제 다음 반복문에서는 n-1 길이의 수열에 대해 갱신된 k번째 숫자를 찾으면 된다. 그 숫자가 정답 수열의 두번째 원소가 되는 것이다.