Skip to content

Commit 65fd1a5

Browse files
committed
Heap
1 parent c47f370 commit 65fd1a5

File tree

5 files changed

+36
-111
lines changed

5 files changed

+36
-111
lines changed

find-median-from-data-stream/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export {default }from "./MedianFinder.ts"
1+
export { default } from "./MedianFinder.ts";

find-median-from-data-stream/test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { assertEquals } from "../deps.ts";
22
import MedianFinder1 from "./index.ts";
33

4-
54
Deno.test("find-median-from-data-stream-1", () => {
65
const medianFinder = new MedianFinder1();
76

@@ -19,7 +18,7 @@ Deno.test("find-median-from-data-stream-2", () => {
1918
assertEquals(medianFinder.findMedian(), 1.5);
2019
medianFinder.addNum(3);
2120
assertEquals(medianFinder.findMedian(), 2);
22-
medianFinder.addNum(3);
21+
medianFinder.addNum(3);
2322
assertEquals(medianFinder.findMedian(), 2.5);
2423
});
2524
Deno.test("find-median-from-data-stream-1", () => {
Lines changed: 18 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import { Heap } from "../deps.ts";
2+
13
export interface PriorityQueue<T = any> {
2-
at(index: number): T | undefined;
34
isEmpty(): boolean;
45

56
clear: () => void;
@@ -12,10 +13,6 @@ export interface PriorityQueue<T = any> {
1213

1314
head: () => T | undefined;
1415

15-
tail: () => T | undefined;
16-
17-
pop: () => T | undefined;
18-
1916
shift: () => T | undefined;
2017
toArray(): T[];
2118
}
@@ -27,101 +24,37 @@ export function PriorityQueue<T = any>(
2724
if (typeof comparator !== "function") {
2825
throw Error("expect comparator to be function");
2926
}
30-
const data: T[] = [];
31-
if (values?.length) {
32-
values.forEach((value) => data.push(value));
33-
data.sort(comparator);
34-
}
35-
function length(): number {
36-
return data.length;
37-
}
38-
function swap(i1: number, i2: number) {
39-
[data[i1], data[i2]] = [data[i2], data[i1]];
40-
}
41-
function bubble_head_to_tail() {
42-
if (data.length < 2) {
43-
return;
44-
}
45-
for (let i = 0, j = 1; j < data.length && i < data.length; i++, j++) {
46-
if (comparator(data[i], data[j]) > 0) {
47-
swap(i, j);
48-
} else {
49-
break;
50-
}
51-
}
52-
}
53-
function bubble_tail_to_head() {
54-
if (data.length < 2) {
55-
return;
56-
}
27+
const data = new Heap<T>((a, b) => comparator(a, b));
28+
values?.forEach((v) => data.push(v));
5729

58-
for (
59-
let i = data.length - 2, j = data.length - 1;
60-
j >= 0 && i >= 0;
61-
i--, j--
62-
) {
63-
if (comparator(data[i], data[j]) > 0) {
64-
swap(i, j);
65-
} else {
66-
break;
67-
}
68-
}
30+
function length(): number {
31+
return data.size();
6932
}
7033

7134
function offer(value: T) {
72-
const f = head();
73-
if (typeof f === "undefined") {
74-
data.push(value);
75-
return;
76-
}
77-
const l = tail();
78-
if (typeof l === "undefined") {
79-
data.push(value);
80-
return;
81-
}
82-
if (comparator(value, l) > 0) {
83-
data.push(value);
84-
} else if (comparator(value, f) < 0) {
85-
data.unshift(value);
86-
} else {
87-
if (comparator(value, data[Math.floor(data.length / 2)]) > 0) {
88-
data.push(value);
89-
bubble_tail_to_head();
90-
} else {
91-
data.unshift(value);
92-
bubble_head_to_tail();
93-
}
94-
}
35+
data.push(value);
9536
}
9637
function head() {
97-
if (!data.length) {
38+
// console.log(data)
39+
if (!data.size()) {
9840
return undefined;
9941
}
100-
return data[0];
101-
}
102-
function tail() {
103-
if (!data.length) {
104-
return undefined;
105-
}
106-
return data[data.length - 1];
107-
}
108-
function pop() {
109-
return data.pop();
42+
return data.top();
11043
}
44+
11145
function shift() {
112-
return data.shift();
46+
// console.log(data)
47+
return data.pop();
11348
}
11449
function clear() {
115-
data.length = 0;
116-
}
117-
function at(index: number): T | undefined {
118-
return data.at(index);
50+
data.clear();
11951
}
52+
12053
function toArray() {
121-
return Array.from(data);
54+
return Array.from(data.sort()).sort(comparator) as T[];
12255
}
12356
function isEmpty() {
124-
return data.length === 0;
57+
return data.isEmpty();
12558
}
12659
return {
12760
isEmpty,
@@ -131,9 +64,7 @@ export function PriorityQueue<T = any>(
13164
comparator,
13265
offer,
13366
head,
134-
tail,
135-
pop,
67+
13668
shift,
137-
at,
13869
};
13970
}

kth-largest-element-in-a-stream/index.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,16 @@ interface KthLargest {
44
add(val: number): number;
55
}
66
function KthLargest(k: number, nums: number[]): KthLargest {
7-
//降序排列
8-
const minheap = PriorityQueue<number>((a, b) => -a + b, nums);
9-
//
10-
// for (const n of nums) {
11-
// minheap.offer(n);
12-
// }
13-
//Array.from(nums).sort((a, b) => -a + b)
7+
const minheap = PriorityQueue<number>((a, b) => a - b, nums);
8+
149
function add(val: number): number {
1510
minheap.offer(val);
16-
// minheap.sort((a, b) => -a + b)
11+
1712
while (minheap.length() > k) {
18-
minheap.pop();
13+
minheap.shift();
1914
}
2015
if (minheap.length()) {
21-
const tail = minheap.tail();
16+
const tail = minheap.head();
2217
if (typeof tail !== "undefined") {
2318
return tail;
2419
}

kth-largest-element-in-a-stream/test.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ Deno.test("PriorityQueue", () => {
113113
8,
114114
47,
115115
];
116-
const maxPQ = PriorityQueue((a, b) => a - b, data);
116+
const maxPQ = PriorityQueue((a, b) => -a + b, data);
117117
assertEquals("function", typeof maxPQ.comparator);
118118
assertEquals(
119119
[
@@ -217,19 +217,19 @@ Deno.test("PriorityQueue", () => {
217217
97,
218218
98,
219219
99,
220-
],
220+
].sort((a, b) => -a + b),
221221
maxPQ.toArray(),
222222
);
223223
assertFalse(maxPQ.isEmpty());
224224
assertEquals(maxPQ.length(), 100);
225-
assertEquals(maxPQ.tail(), 99);
225+
// assertEquals(maxPQ.tail(), 99);
226226
assertEquals(maxPQ.head(), 0);
227-
assertEquals(maxPQ.pop(), 99);
228-
assertEquals(maxPQ.length(), 99);
229227
assertEquals(maxPQ.shift(), 0);
228+
assertEquals(maxPQ.length(), 99);
229+
assertEquals(maxPQ.shift(), 99);
230230
assertEquals(maxPQ.length(), 98);
231-
assertEquals(maxPQ.tail(), 98);
232-
assertEquals(maxPQ.head(), 1);
231+
// assertEquals(maxPQ.tail(), 98);
232+
assertEquals(maxPQ.head(), 98);
233233
maxPQ.offer(1000);
234234
maxPQ.offer(-1000);
235235
assertEquals(
@@ -334,10 +334,10 @@ Deno.test("PriorityQueue", () => {
334334
97,
335335
98,
336336
1000,
337-
],
337+
].sort((a, b) => -a + b),
338338
maxPQ.toArray(),
339339
);
340-
assertEquals(maxPQ.tail(), 1000);
340+
// assertEquals(maxPQ.tail(), 1000);
341341
assertEquals(maxPQ.head(), -1000);
342342
maxPQ.offer(50);
343343
assertEquals(
@@ -443,14 +443,14 @@ Deno.test("PriorityQueue", () => {
443443
97,
444444
98,
445445
1000,
446-
],
446+
].sort((a, b) => -a + b),
447447
maxPQ.toArray(),
448448
);
449449
assertEquals(maxPQ.length(), 101);
450450

451451
const length = maxPQ.length();
452452
for (let i = 0; i < length; i++) {
453-
assert(typeof maxPQ.pop() === "number");
453+
assert(typeof maxPQ.shift() === "number");
454454
}
455455
assert(maxPQ.isEmpty());
456456
assertEquals(0, maxPQ.length());

0 commit comments

Comments
 (0)