We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5b6b238 commit 815f244Copy full SHA for 815f244
60-permutation-sequence.js
@@ -1,3 +1,39 @@
1
+/**
2
+ * @param {number} n
3
+ * @param {number} k
4
+ * @return {string}
5
+ */
6
+const getPermutation = function(n, k) {
7
+ const factorial = Array(n + 1).fill(0)
8
+ factorial[0] = 1
9
+ for(let i = 1; i <= n; i++) {
10
+ factorial[i] = factorial[i - 1] * i
11
+ }
12
+ let res = ''
13
+ const visited = Array(n + 1).fill(0)
14
+ dfs(0)
15
+ return res
16
+
17
+ function dfs(idx) {
18
+ if(idx === n) return
19
20
+ const cnt = factorial[n - idx - 1]
21
22
+ if(visited[i]) continue
23
+ if(cnt < k) {
24
+ k -= cnt
25
+ continue
26
27
+ res += i
28
+ visited[i] = 1
29
+ dfs(idx + 1)
30
+ return
31
32
33
+};
34
35
+// another
36
37
/**
38
* @param {number} n
39
* @param {number} k
0 commit comments