본문 바로가기
Coding Test/Programmers

[Programmers] Lv 2. 줄 서는 방법

by song.ift 2023. 3. 12.

https://school.programmers.co.kr/learn/courses/30/lessons/12936

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

function solution(n, k) {
    const [arr, result, dp] = [
        Array.from({ length: n }, (_, i) => i + 1),
        [],
        [1, 1, 2],
    ];
    const factorial = (n) => {
        if (dp[n] !== undefined) return dp[n];
        return (dp[n] = n * factorial(n - 1));
    };

    for (let i = 0; i < n; ++i) {
        const fac = factorial(arr.length - 1);
        const idx = Math.ceil(k / fac) - 1;
        k = k - idx * fac;

        result.push(arr[idx]);
        arr.splice(idx, 1);
    }
    return result;
}

GitHub : https://github.com/developeSHG/Algorithm-Baekjoon_Programmers/tree/main/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4/lv2/12936.%E2%80%85%EC%A4%84%E2%80%85%EC%84%9C%EB%8A%94%E2%80%85%EB%B0%A9%EB%B2%95

 

GitHub - developeSHG/Algorithm-Baekjoon_Programmers: 백준 and 프로그래머스 소스코드

백준 and 프로그래머스 소스코드. Contribute to developeSHG/Algorithm-Baekjoon_Programmers development by creating an account on GitHub.

github.com

 

댓글