-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathpermutations-iv.cpp
69 lines (66 loc) · 1.97 KB
/
permutations-iv.cpp
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Time: O(n^2)
// Space: O(n)
// combinatorics
class Solution {
public:
vector<int> permute(int n, long long k) {
vector<int> result;
vector<int64_t> cnt(n + 1, 1);
for (int i = 1; i + 1 < size(cnt); ++i) {
cnt[i + 1] = min(cnt[i] * ((i + 2) / 2), static_cast<int64_t>(k));
}
vector<bool> lookup(n);
for (int i = 0; i < n; ++i) {
int j = 0;
for (; j < n; ++j) {
if (!(!lookup[j] && ((i == 0 && n % 2 == 0) || ((j + 1) % 2 == (!empty(result) ? (result.back() % 2) ^ 1 : 1))))) {
continue;
}
if (k <= cnt[n - 1 - i]) {
break;
}
k -= cnt[n - 1 - i];
}
if (j == n) {
return {};
}
lookup[j] = true;
result.emplace_back(j + 1);
}
return result;
}
};
// Time: O(n^2)
// Space: O(n)
// combinatorics
class Solution2 {
public:
vector<int> permute(int n, long long k) {
typedef __int128 int128_t;
vector<int> result;
vector<int128_t> fact((((n - 1) + 1) / 2) + 1, 1);
for (int i = 0; i + 1 < size(fact); ++i) {
fact[i + 1] = min(fact[i] * (i + 1), static_cast<int128_t>(k));
}
vector<bool> lookup(n);
for (int i = 0; i < n; ++i) {
const auto cnt = fact[(n - 1 - i) / 2] * fact[((n - 1 - i) + 1) / 2];
int j = 0;
for (; j < n; ++j) {
if (!(!lookup[j] && ((i == 0 && n % 2 == 0) || ((j + 1) % 2 == (!empty(result) ? (result.back() % 2) ^ 1 : 1))))) {
continue;
}
if (k <= cnt) {
break;
}
k -= cnt;
}
if (j == n) {
return {};
}
lookup[j] = true;
result.emplace_back(j + 1);
}
return result;
}
};