deno.land / x / masx200_leetcode_test@10.6.5 / kth-largest-element-in-a-stream / index.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
import { PriorityQueue } from "./PriorityQueue.ts";
interface KthLargest { add(val: number): number;}function KthLargest(k: number, nums: number[]): KthLargest { const minheap = PriorityQueue<number>((a, b) => a - b, nums);
function add(val: number): number { minheap.offer(val);
while (minheap.length() > k) { minheap.shift(); } if (minheap.length()) { const tail = minheap.head(); if (typeof tail !== "undefined") { return tail; } } throw Error("pop empty heap"); } return { add };}export default KthLargest;
masx200_leetcode_test

Version Info

Tagged at
a year ago