기본적인 순열과 조합문제이다.
몫과 나머지, 순열에 따라 순서가 정해진다.
#include <string>
#include <vector>
using namespace std;
long long facto(long long num) {
long long n = 1;
for (int i = 2; i <= num; i++)
n *= i;
return n;
}
vector<int> solution(int n, long long k) {
vector<int> answer, vt;
for (int i = 1; i <= n; i++)
vt.push_back(i);
if (k == 1)
return vt;
k--;
long long pre = facto(n);
long long mod, div;
while (vt.size() != 1) {
pre /= n;
mod = k % pre;
div = k / pre;
answer.push_back(vt[div]);
vt.erase(vt.begin() + div);
k = mod;
n--;
}
answer.push_back(vt[0]);
return answer;
}'Coding_Test 연습 > Programmers' 카테고리의 다른 글
| [프로그래머스] (C++) LV2 N-Queen (0) | 2022.06.11 |
|---|---|
| [프로그래머스] (C++) LV2 하노이의 탑 (0) | 2022.06.11 |
| [프로그래머스] (C++) LV2 숫자 블록 (0) | 2022.06.10 |
| [프로그래머스] (C++) LV2 멀리 뛰기 (0) | 2022.06.09 |
| [프로그래머스] (C++) LV2 쿼드압축 후 개수 세기 (0) | 2022.06.08 |