-
Notifications
You must be signed in to change notification settings - Fork 31
/
combinatorics.ts
411 lines (380 loc) · 10.1 KB
/
combinatorics.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/* eslint-disable no-lone-blocks */
/* eslint-disable @typescript-eslint/no-empty-function */
/* eslint-disable no-inner-declarations */
/**
* 遍历子集.
*
* @complexity O(2^n), 2^27(1.3e8) => 1s.
*/
function powerset(n: number, f: (subset: readonly number[]) => boolean | void): void {
const path: number[] = []
dfs(0)
function dfs(index: number): boolean {
if (index === n) return !!f(path)
if (dfs(index + 1)) return true
path.push(index)
if (dfs(index + 1)) return true
path.pop()
return false
}
}
/**
* 遍历数组所有的分割方案,按照分割点将数组分割成若干段.
*
* @complexity O(2^n), 2^27(1.3e8) => 630ms.
* @example
* ```ts
* partitions(3, splits => {
* for (let i = 0; i < splits.length - 1; i++) {
* const start = splits[i]
* const end = splits[i + 1]
* console.log(arr.slice(start, end))
* }
* })
* ```
*/
function partitions(n: number, f: (splits: readonly number[]) => boolean | void): void {
if (!n) return
const path: number[] = [0]
dfs(0)
function dfs(index: number): boolean {
if (index === n - 1) {
path.push(n)
const stop = f(path)
path.pop()
return !!stop
}
if (dfs(index + 1)) return true
path.push(index + 1)
if (dfs(index + 1)) return true
path.pop()
return false
}
}
/**
* 将 {@link n} 个元素的集合分成 {@link k} 个部分,不允许为空.
*
* @complexity n=13,k=5 => 1e7、250ms.
* @note 对每个元素,是放入之前的部分还是新建一个部分.
*/
function setPartitions(
n: number,
k: number,
f: (parts: readonly number[][]) => boolean | void
): void {
if (k < 1) throw new Error("Can't partition in a negative or zero number of groups")
if (k > n) return
const parts: number[][] = Array(k)
for (let i = 0; i < k; i++) parts[i] = []
dfs(0, 0)
function dfs(index: number, count: number): boolean {
if (index === n) {
if (count === k) {
return !!f(parts)
}
return false
}
for (let i = 0; i < count; i++) {
parts[i].push(index)
if (dfs(index + 1, count)) return true
parts[i].pop()
}
if (count < k) {
parts[count].push(index)
if (dfs(index + 1, count + 1)) return true
parts[count].pop()
}
return false
}
}
/**
* 将 {@link n} 个元素的集合分成任意个部分.
*
* @param f 分成 `k` 个部分,parts.slice(0, k) 为分割结果. 返回 `true` 则停止搜索.
* @complexity n=13 => 2e7、250ms.
*/
function setPartitionsAll(
n: number,
f: (parts: readonly number[][], k: number) => boolean | void
): void {
const parts: number[][] = Array(n)
for (let i = 0; i < n; i++) parts[i] = []
dfs(0, 0)
function dfs(index: number, count: number): boolean {
if (index === n) {
return !!f(parts, count)
}
for (let i = 0; i < count; i++) {
parts[i].push(index)
if (dfs(index + 1, count)) return true
parts[i].pop()
}
parts[count].push(index)
if (dfs(index + 1, count + 1)) return true
parts[count].pop()
return false
}
}
/**
* 遍历无重复排列.
*
* @complexity 11!(4e7) => 200ms.
*/
function distinctPermutations<T extends number | string>(
arr: ArrayLike<T>,
r: number,
f: (perm: readonly T[]) => boolean | void
): void {
if (!arr.length || r < 1 || r > arr.length) return
const copy = Array.from(arr)
copy.sort((a, b) => {
if (a < b) return -1
if (a > b) return 1
return 0
})
if (r === arr.length) {
while (true) {
if (f(copy)) break
if (!nextPermutation(copy)) break
}
return
}
const head = copy.slice(0, r)
const tail = copy.slice(r)
while (tail.length < arr.length) tail.push(arr[0])
const tailLen = arr.length - r
while (true) {
if (f(head)) return
let pivot = tail[tailLen - 1]
let i = r - 1
let found = false
for (; ~i; i--) {
if (head[i] < pivot) {
found = true
break
}
pivot = head[i]
}
if (!found) return
found = false
for (let j = 0; j < tailLen; j++) {
if (tail[j] > head[i]) {
const tmp = head[i]
head[i] = tail[j]
tail[j] = tmp
found = true
break
}
}
if (!found) {
for (let j = r - 1; ~j; j--) {
if (head[j] > head[i]) {
const tmp = head[i]
head[i] = head[j]
head[j] = tmp
break
}
}
}
// reversr head[i + 1:] and swap it with tail[:r - (i + 1)]
for (let j = tailLen, k = r - 1; k > i; k--, j++) {
tail[j] = head[k]
}
for (let j = i + 1, k = 0; j < r; j++, k++) {
head[j] = tail[k]
}
for (let j = 0, k = r - i - 1; j < tailLen; j++, k++) {
tail[j] = tail[k]
}
}
/** full. */
function nextPermutation(arr: T[]): boolean {
if (!arr.length) return false
let left = arr.length - 1
while (left > 0 && arr[left - 1] >= arr[left]) left--
if (!left) return false
const last = left - 1
let right = arr.length - 1
while (arr[right] <= arr[last]) right--
const tmp = arr[last]
arr[last] = arr[right]
arr[right] = tmp
for (let i = last + 1, j = arr.length - 1; i < j; i++, j--) {
const tmp = arr[i]
arr[i] = arr[j]
arr[j] = tmp
}
return true
}
}
/**
* 遍历无重复组合.
*
* @complexity C(30,10)(3e7) => 400ms.
* @example
* ```ts
* distinctCombinations([0, 0, 1, 0], 2, console.log) // [0, 0], [0, 1], [1, 0]
* ```
*/
function distinctCombinations<T extends number | string>(
arr: ArrayLike<T>,
r: number,
f: (comb: readonly T[]) => boolean | void
): void {
if (!arr.length || r < 1 || r > arr.length) return
const uniqueEverSeen: number[][] = Array(arr.length)
for (let i = 0; i < arr.length; i++) {
const indexes: number[] = []
const visited = new Set<T>()
for (let j = i; j < arr.length; j++) {
if (visited.has(arr[j])) continue
visited.add(arr[j])
indexes.push(j)
}
uniqueEverSeen[i] = indexes
}
const path: T[] = []
function dfs(index: number, count: number): boolean {
if (count === r) {
return !!f(path)
}
if (index === arr.length) return false
const indexes = uniqueEverSeen[index]
for (let i = 0; i < indexes.length; i++) {
const curIndex = indexes[i]
path.push(arr[indexes[i]])
if (dfs(curIndex + 1, count + 1)) return true
path.pop()
}
return false
}
dfs(0, 0)
}
export {
powerset,
partitions,
setPartitions,
setPartitionsAll,
distinctPermutations,
distinctCombinations
}
if (require.main === module) {
// 46. 全排列
// https://leetcode.cn/problems/permutations/
function permute(nums: number[]): number[][] {
const res: number[][] = []
distinctPermutations(nums, nums.length, perm => {
res.push(perm.slice())
})
return res
}
// 78. 子集
// https://leetcode.cn/problems/subsets/description/
function subsets(nums: number[]): number[][] {
const res: number[][] = []
powerset(nums.length, subset => {
const cur = Array<number>(subset.length)
for (let i = 0; i < subset.length; i++) {
cur[i] = nums[subset[i]]
}
res.push(cur)
})
return res
}
// 2698. 求一个整数的惩罚数
// https://leetcode.cn/problems/find-the-punishment-number-of-an-integer/
function punishmentNumber(n: number): number {
/** v * v 的十进制表示的字符串可以分割成若干连续子字符串,且这些子字符串对应的整数值之和等于 v 。 */
const toDigit = (nums: number[], start: number, end: number): number => {
let res = 0
for (let i = start; i < end; i++) res = res * 10 + nums[i]
return res
}
const check = (v: number): boolean => {
const nums = String(v * v)
.split('')
.map(Number)
let ok = false
partitions(nums.length, splits => {
let sum = 0
for (let i = 0; i < splits.length - 1; i++) {
sum += toDigit(nums, splits[i], splits[i + 1])
}
if (sum === v) {
ok = true
return true
}
})
return ok
}
let res = 0
for (let i = 1; i <= n; i++) {
if (check(i)) res += i * i
}
return res
}
function test1() {
console.time('powerset')
powerset(27, subset => {})
console.timeEnd('powerset')
console.time('partitions')
partitions(27, splits => {})
console.timeEnd('partitions')
const arr = 'abc'
partitions(arr.length, splits => {
const cur = []
for (let i = 0; i < splits.length - 1; i++) {
const start = splits[i]
const end = splits[i + 1]
cur.push(arr.slice(start, end))
}
console.log(cur)
})
let count = 0
console.time('setPartitions')
setPartitions(13, 6, parts => {
count++
})
console.timeEnd('setPartitions')
console.log(count)
count = 0
console.time('setPartitionsAll')
setPartitionsAll(3, (parts, k) => {
console.log(parts.slice(0, k))
})
console.log(count)
console.timeEnd('setPartitionsAll')
}
console.time('distinctPermutations')
const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let count = 0
distinctPermutations(arr, arr.length - 1, perm => {
count++
})
console.log(count)
console.timeEnd('distinctPermutations')
console.time('distinctCombinations')
count = 0
distinctCombinations(
Array.from({ length: 30 }, (_, i) => i),
10,
comb => {
count++
}
)
console.log(count)
console.timeEnd('distinctCombinations')
// {
// let count = 0
// console.time('setPartitions')
// setPartitions(13, 6, parts => {
// count++
// })
// console.log(count)
// console.timeEnd('setPartitions')
// }
setPartitions(3, 2, parts => {
console.log(parts)
})
console.log('done')
}