-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathIntervalHeap.ts
196 lines (177 loc) · 4.91 KB
/
IntervalHeap.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
/* eslint-disable @typescript-eslint/no-non-null-assertion */
// https://natsugiri.hatenablog.com/entry/2016/10/10/035445
import assert from 'assert'
type Comparator<T> = (a: T, b: T) => number
/**
* !维护最大值和最小值的堆.
*/
class IntervalHeap<E = number> {
private readonly _data: E[]
private readonly _comparator: Comparator<E>
constructor()
constructor(array: E[])
constructor(comparator: Comparator<E>)
constructor(array: E[], comparator: Comparator<E>)
constructor(comparator: Comparator<E>, array: E[])
constructor(arrayOrComparator1?: E[] | Comparator<E>, arrayOrComparator2?: E[] | Comparator<E>) {
let defaultArray: E[] = []
let defaultComparator = (a: E, b: E) => (a as unknown as number) - (b as unknown as number)
if (arrayOrComparator1) {
if (Array.isArray(arrayOrComparator1)) {
defaultArray = arrayOrComparator1
} else {
defaultComparator = arrayOrComparator1
}
}
if (arrayOrComparator2) {
if (Array.isArray(arrayOrComparator2)) {
defaultArray = arrayOrComparator2
} else {
defaultComparator = arrayOrComparator2
}
}
this._comparator = defaultComparator
this._data = defaultArray
if (this._data.length) this._heapify()
}
push(value: E): void {
const k = this._data.length
this._data.push(value)
this._pushUp(k)
}
popMax(): E | undefined {
const res = this.max
if (this._data.length < 2) {
this._data.pop()
return res
}
this._data[0] = this._data.pop()!
const k = this._pushDown(0)
this._pushUp(k)
return res
}
popMin(): E | undefined {
const res = this.min
if (this._data.length < 3) {
this._data.pop()
return res
}
this._data[1] = this._data.pop()!
const k = this._pushDown(1)
this._pushUp(k)
return res
}
get size(): number {
return this._data.length
}
get max(): E | undefined {
return this._data[0]
}
get min(): E | undefined {
return this._data.length < 2 ? this._data[0] : this._data[1]
}
private _heapify(): void {
for (let i = this._data.length - 1; ~i; i--) {
if (i & 1 && this._comparator(this._data[i - 1], this._data[i]) < 0) {
const tmp = this._data[i - 1]
this._data[i - 1] = this._data[i]
this._data[i] = tmp
}
const k = this._pushDown(i)
this._pushUp(k, i)
}
}
private _pushUp(k: number, root = 1): number {
const a = k & ~1
const b = k | 1
const data = this._data
if (b < data.length && this._comparator(data[a], data[b]) < 0) {
const tmp = data[a]
data[a] = data[b]
data[b] = tmp
k ^= 1
}
let p = 0
for (; root < k; k = p) {
p = ((k >> 1) - 1) & ~1 // parent(k)
if (this._comparator(data[p], data[k]) >= 0) break
const tmp = data[p]
data[p] = data[k]
data[k] = tmp
}
for (; root < k; k = p) {
p = (((k >> 1) - 1) & ~1) | 1
if (this._comparator(data[k], data[p]) >= 0) break
const tmp = data[p]
data[p] = data[k]
data[k] = tmp
}
return k
}
private _pushDown(k: number): number {
const n = this._data.length
const data = this._data
if (k & 1) {
while (((k << 1) | 1) < n) {
let c = (k << 1) + 3
if (n <= c || this._comparator(data[c - 2], data[c]) < 0) {
c -= 2
}
if (c < n && this._comparator(data[c], data[k]) < 0) {
const tmp = data[k]
data[k] = data[c]
data[c] = tmp
k = c
} else {
break
}
}
} else {
while ((k << 1) + 2 < n) {
let c = (k << 1) + 4
if (n <= c || this._comparator(data[c], data[c - 2]) < 0) {
c -= 2
}
if (c < n && this._comparator(data[k], data[c]) < 0) {
const tmp = data[k]
data[k] = data[c]
data[c] = tmp
k = c
} else {
break
}
}
}
return k
}
}
export { IntervalHeap }
if (require.main === module) {
const pq = new IntervalHeap<number>((a, b) => a - b, [-3, 0, 1, 3])
pq.push(3)
assert.strictEqual(pq.popMax(), 3)
assert.strictEqual(pq.popMax(), 3)
pq.push(-2)
pq.push(1)
assert.strictEqual(pq.popMin(), -3)
assert.strictEqual(pq.popMin(), -2)
assert.strictEqual(pq.popMax(), 1)
assert.strictEqual(pq.popMin(), 0)
assert.strictEqual(pq.popMax(), 1)
console.log('OK')
// time
const n = 5e5
const pq2 = new IntervalHeap<number>((a, b) => a - b)
console.time('IntervalHeap')
for (let i = 0; i < n; i++) {
pq2.push(i)
pq2.push(i)
}
for (let i = 0; i < n; i++) {
pq2.popMax()
pq2.popMin()
pq.min
pq.max
}
console.timeEnd('IntervalHeap') // IntervalHeap: 310.506ms
}