-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmax-pq.ts
60 lines (51 loc) · 1.04 KB
/
max-pq.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
/**
* 基于堆的优先序列
*/
export default class MaxPQ<T> {
private pq: T[]
private N: number
constructor() {
this.pq = []
this.N = 0
}
isEmpty(): boolean {
return this.N === 0
}
size(): number {
return this.N
}
insert(v: T) {
this.pq[++this.N] = v
this.swim(this.N)
}
delMax(): T {
const max = this.pq[1]
this.exch(1, this.N--)
this.pq[this.N + 1] = null
this.sink(1)
return max
}
private exch(i: number, j: number) {
const temp = this.pq[i]
this.pq[i] = this.pq[j]
this.pq[j] = temp
}
private less(i: number, j: number) {
return this.pq[i] < this.pq[j]
}
private swim(k: number) {
while (k > 1 && this.less(Math.floor(k / 2), k)) {
this.exch(Math.floor(k / 2), k)
k = Math.floor(k / 2)
}
}
private sink(dad: number) {
while (2 * dad <= this.N) {
let son = 2 * dad
if (son < this.N && this.less(son, son + 1)) son++
if (!this.less(dad, son)) break
this.exch(dad, son)
dad = son
}
}
}