-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path060.js
More file actions
31 lines (27 loc) · 641 Bytes
/
Copy path060.js
File metadata and controls
31 lines (27 loc) · 641 Bytes
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
/**
* @param {number} n
* @param {number} k
* @return {string}
*/
var getPermutation = function(n, k) {
let candidates = [];
for (let i = 1; i <= n; ++i) {
candidates.push(i);
}
const factorial = n => {
if (n === 1 || n === 0) return 1;
return n * factorial(n - 1);
}
let result = "";
let count = n;
while(candidates.length > 0) {
const count_factorial = factorial(count - 1);
const index = parseInt((k - 1) / count_factorial);
result = result + candidates[index];
candidates.splice(index, 1);
k = k - index * count_factorial;
--count;
}
return result;
};
getPermutation(1, 1);