-
Notifications
You must be signed in to change notification settings - Fork 31
/
permutations.ts
111 lines (105 loc) · 2.54 KB
/
permutations.ts
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* eslint-disable no-lone-blocks */
/* eslint-disable no-constant-condition */
/* eslint-disable no-param-reassign */
/**
* 按照字典序遍历所有大小为`r`的排列.
* @param n 排列总元素个数.
* @param r 选取的元素个数.
* @param f 回调函数, 用于处理每个排列的结果.返回`true`时停止遍历.
* @example
* ```ts
* enumeratePermutations(3, 2, perm => {
* console.log(perm)
* })
* // [ 0, 1 ]
* // [ 0, 2 ]
* // [ 1, 0 ]
* // [ 1, 2 ]
* // [ 2, 0 ]
* // [ 2, 1 ]
* ```
* @complexity 11!(4e7) => 2.2ms
*/
function enumeratePermutations(
n: number,
r: number,
f: (indicesView: readonly number[]) => boolean | void
): void {
const visited = new Uint8Array(n)
const dfs = (path: number[]): boolean => {
if (path.length === r) {
return !!f(path)
}
for (let i = 0; i < n; i++) {
if (visited[i]) continue
visited[i] = 1
path.push(i)
if (dfs(path)) return true
visited[i] = 0
path.pop()
}
return false
}
dfs([])
}
/**
* 无序遍历全排列.
* @param n 全排列的长度.
* @param f 回调函数, 用于处理每个排列的结果.返回`true`时停止遍历.
* @example
* ```ts
* enumeratePermutationsAll(3, perm => {
* console.log(perm)
* })
* // [ 0, 1, 2 ]
* // [ 0, 2, 1 ]
* // [ 1, 0, 2 ]
* // [ 1, 2, 0 ]
* // [ 2, 1, 0 ]
* // [ 2, 0, 1 ]
* ```
* @complexity 11!(4e7) => 570ms
*/
function enumeratePermutationsAll(
n: number,
f: (indicesView: readonly number[]) => boolean | void
): void {
const dfs = (a: number[], i: number): boolean => {
if (i === a.length) {
return !!f(a)
}
dfs(a, i + 1)
for (let j = i + 1; j < a.length; j++) {
const tmp = a[i]
a[i] = a[j]
a[j] = tmp
if (dfs(a, i + 1)) return true
a[j] = a[i]
a[i] = tmp
}
return false
}
const ids = Array.from({ length: n }, (_, i) => i)
dfs(ids, 0)
}
if (require.main === module) {
{
console.time('enumeratePermutations')
let count = 0
enumeratePermutations(11, 11, indices => {
count++
})
console.timeEnd('enumeratePermutations') // 2.2s
console.log(count) // 39916800
}
{
console.time('enumeratePermutationsAll')
let count = 0
enumeratePermutationsAll(11, indices => {
count++
})
console.timeEnd('enumeratePermutationsAll') // 600ms
console.log(count) // 39916800
}
}
export { enumeratePermutations, enumeratePermutationsAll }