-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathcombinationsWithReplacement.ts
64 lines (60 loc) · 1.34 KB
/
combinationsWithReplacement.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
/* eslint-disable no-constant-condition */
/**
* 可取重复元素的组合,遍历所有大小为`r`的组合.
* @param n 元素总数.
* @param r 选取的元素个数.
* @param f 回调函数, 用于处理每个组合的结果.返回`true`时停止遍历.
* @example
* ```ts
* enumerateCombinationsWithReplacement(3, 2, indices => {
* console.log(indices)
* })
* // [ 0, 0 ]
* // [ 0, 1 ]
* // [ 0, 2 ]
* // [ 1, 1 ]
* // [ 1, 2 ]
* // [ 2, 2 ]
* ```
* @complexity 2e7 => 100ms.
*/
function enumerateCombinationsWithReplacement<C>(
n: number,
r: number,
f: (indicesView: readonly number[]) => boolean | void
): void {
const ids = Array(r).fill(0)
if (f(ids)) {
return
}
while (true) {
let i = r - 1
for (; i >= 0; i--) {
if (ids[i] !== n - 1) {
break
}
}
if (i === -1) {
return
}
ids[i]++
for (let j = i + 1; j < r; j++) {
ids[j] = ids[i]
}
if (f(ids)) {
return
}
}
}
if (require.main === module) {
const n = 20
const r = 10
console.time('combinations')
let count = 0
enumerateCombinationsWithReplacement(n, r, indices => {
count++
})
console.timeEnd('combinations') // !100ms
console.log(count) // !20030010
}
export { enumerateCombinationsWithReplacement }