-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgetPermutation.go
More file actions
60 lines (50 loc) · 1.12 KB
/
Copy pathgetPermutation.go
File metadata and controls
60 lines (50 loc) · 1.12 KB
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
/* https://leetcode.com/problems/permutation-sequence/description/
The set [1,2,3,…,n] contains a total of n! unique permutations.
By listing and labeling all of the permutations in order,
We get the following sequence (ie, for n = 3):
"123"
"132"
"213"
"231"
"312"
"321"
Given n and k, return the kth permutation sequence.
Note: Given n will be between 1 and 9 inclusive.
*/
package lbacktracking
import (
"strconv"
"strings"
)
func getPermutation(n int, k int) string {
factorial := func(n int) int {
sum := 1
for n > 1 {
sum *= n
n--
}
return sum
}
var helper func(arr []string, k int) string
helper = func(arr []string, k int) string {
if k == 1 {
return strings.Join(arr, "")
}
subN := factorial(len(arr) - 1)
index := k / subN
if k%subN == 0 {
index--
}
tmpArr := []string{}
tmpArr = append(tmpArr, arr[:index]...)
if tmp := index + 1; tmp < len(arr) {
tmpArr = append(tmpArr, arr[tmp:]...)
}
return arr[index] + helper(tmpArr, k-subN*index)
}
arr := make([]string, n, n)
for i := 1; i <= n; i++ {
arr[i-1] = strconv.Itoa(i)
}
return helper(arr, k)
}