Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: 새로운 해설 4개 추가 #98

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

Merged
codeisneverodd merged 5 commits into main from codeisneverodd
Sep 6, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: 2 / 줄 서는 방법 / 새로운 문제
  • Loading branch information
codeisneverodd committed Sep 6, 2022
commit a5a3f97b512005cd41eaefcb9f5bb8a2ac4ef868
39 changes: 39 additions & 0 deletions level-2/줄-서는-방법.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//https://github.com/codeisneverodd/programmers-coding-test
//더 좋은 풀이가 존재할 수 있습니다.
//정답 1 - codeisneverodd
function solution(n, k) {
const getFactorial = n => {
const result = [1, 1, 2, ...Array(n - 2)];
result.forEach((_, i) => {
if (i > 2) result[i] = result[i - 1] * i;
});
return result;
};

const getDivision = (dividend, divisor) => {
const quotient = Math.floor(dividend / divisor);
const remainder = dividend % divisor;
return [quotient, remainder];
};

const stepCount = getFactorial(n).reverse();

const generateSteps = (k, step) => {
const [q, r] = getDivision(k, stepCount[step]);
if (r === 0) return [q];
return [q, ...generateSteps(r, step + 1)];
};

const answer = [];

const steps = generateSteps(k - 1, 0);

const notUsedNums = Array.from({ length: n }, (_, i) => i + 1);

steps.slice(1).forEach(q => {
answer.push(notUsedNums[q]);
notUsedNums.splice(q, 1);
});

return [...answer, ...notUsedNums];
}

AltStyle によって変換されたページ (->オリジナル) /