deno.land / x / masx200_leetcode_test@10.6.5 / kth-largest-element-in-a-stream / PriorityQueue.ts

PriorityQueue.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
import { Heap } from "../deps.ts";
export interface PriorityQueue<T = any> { isEmpty(): boolean;
clear: () => void;
length: () => number;
comparator: (a: T, b: T) => number;
offer: (value: T) => void;
head: () => T | undefined;
shift: () => T | undefined; toArray(): T[];}
export function PriorityQueue<T = any>( comparator: (a: T, b: T) => number, values?: T[],): PriorityQueue<T> { if (typeof comparator !== "function") { throw Error("expect comparator to be function"); } const data = new Heap<T>((a, b) => comparator(a, b)); values?.forEach((v) => data.push(v));
function length(): number { return data.size(); }
function offer(value: T) { data.push(value); } function head() { // console.log(data) if (!data.size()) { return undefined; } return data.top(); }
function shift() { // console.log(data) return data.pop(); } function clear() { data.clear(); }
function toArray() { return Array.from(data.sort()).sort(comparator) as T[]; } function isEmpty() { return data.isEmpty(); } return { isEmpty, toArray, clear, length, comparator, offer, head,
shift, };}
masx200_leetcode_test

Version Info

Tagged at
a year ago