deno.land / x / masx200_leetcode_test@10.6.5 / find-median-from-data-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
27
28
29
30
31
32
33
34
35
import { Heap } from "../deps.ts";
export default class MedianFinder { #min: Heap<number>; #max: Heap<number>; constructor() { this.#min = new Heap((a, b) => a - b); this.#max = new Heap((a, b) => b - a); } addNum(num: number) { const max = this.#max, min = this.#min; const m = max.size(), n = min.size(); if (m == n) { // 两个堆数目相等:m=n min.insert(num); max.insert(min.pop()); } else { // 两个堆数目不相等:m=n+1 max.insert(num); min.insert(max.pop()); } } findMedian() { const max = this.#max, min = this.#min; if (max.size() === min.size()) { return (max.top() + min.top()) / 2; } else { return max.top(); } }}
masx200_leetcode_test
leetcode题解和一些测试工具
DocumentationDocumentation
GitHub Repositorymasx200/leetcode-test
GitHub Stars
4

Version Info

Tagged at
4 months ago