-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.h
46 lines (43 loc) · 1.04 KB
/
solution.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
Code generated by https://github.com/goodstudyqaq/leetcode-local-tester
*/
#if __has_include("../utils/cpp/help.hpp")
#include "../utils/cpp/help.hpp"
#elif __has_include("../../utils/cpp/help.hpp")
#include "../../utils/cpp/help.hpp"
#else
#define debug(...) 42
#endif
const int maxn = 10;
long long fact[maxn];
void init() {
fact[0] = 1;
for (int i = 1; i < maxn; i++) {
fact[i] = fact[i - 1] * i;
}
}
class Solution {
public:
string getPermutation(int n, int k) {
init();
k--;
vector<int> rest(n, 0);
iota(rest.begin(), rest.end(), 1);
vector<int> A(n + 1);
for (int i = 1; i <= n; i++) {
A[i] = k / fact[n - i];
k = k % fact[n - i];
}
debug(A);
for (int i = 1; i <= n; i++) {
A[i] = rest[A[i]];
rest.erase(lower_bound(rest.begin(), rest.end(), A[i]));
}
debug(A);
string ans = "";
for (int i = 1; i <= n; i++) {
ans += '0' + A[i];
}
return ans;
}
};