@ -3637,11 +3637,49 @@
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >
< div class = "highlight" > < span class = "filename" > top_k.js< / span > < pre > < span > < / span > < code > < a id = "__codelineno-6-1" name = "__codelineno-6-1" href = "#__codelineno-6-1" > < / a > < span class = "p" > [< / span > < span class = "kd" > class< / span > < span class = "p" > ]{}< / span > < span class = "o" > -< / span > < span class = "p" > [< / span > < span class = "nx" > func< / span > < span class = "p" > ]{< / span > < span class = "nx" > topKHeap< / span > < span class = "p" > }< / span >
< div class = "highlight" > < span class = "filename" > top_k.js< / span > < pre > < span > < / span > < code > < a id = "__codelineno-6-1" name = "__codelineno-6-1" href = "#__codelineno-6-1" > < / a > < span class = "cm" > /* 基于堆查找数组中最大的 k 个元素 */< / span >
< a id = "__codelineno-6-2" name = "__codelineno-6-2" href = "#__codelineno-6-2" > < / a > < span class = "kd" > function< / span > < span class = "w" > < / span > < span class = "nx" > topKHeap< / span > < span class = "p" > (< / span > < span class = "nx" > nums< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > k< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-6-3" name = "__codelineno-6-3" href = "#__codelineno-6-3" > < / a > < span class = "w" > < / span > < span class = "c1" > // 使用大顶堆 MaxHeap, 对数组 nums 取相反数< / span >
< a id = "__codelineno-6-4" name = "__codelineno-6-4" href = "#__codelineno-6-4" > < / a > < span class = "w" > < / span > < span class = "kd" > const< / span > < span class = "w" > < / span > < span class = "nx" > invertedNums< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "nx" > nums< / span > < span class = "p" > .< / span > < span class = "nx" > map< / span > < span class = "p" > ((< / span > < span class = "nx" > num< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > => < / span > < span class = "w" > < / span > < span class = "o" > -< / span > < span class = "nx" > num< / span > < span class = "p" > );< / span >
< a id = "__codelineno-6-5" name = "__codelineno-6-5" href = "#__codelineno-6-5" > < / a > < span class = "w" > < / span > < span class = "c1" > // 将数组的前 k 个元素入堆< / span >
< a id = "__codelineno-6-6" name = "__codelineno-6-6" href = "#__codelineno-6-6" > < / a > < span class = "w" > < / span > < span class = "kd" > const< / span > < span class = "w" > < / span > < span class = "nx" > heap< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "ow" > new< / span > < span class = "w" > < / span > < span class = "nx" > MaxHeap< / span > < span class = "p" > (< / span > < span class = "nx" > invertedNums< / span > < span class = "p" > .< / span > < span class = "nx" > slice< / span > < span class = "p" > (< / span > < span class = "mf" > 0< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > k< / span > < span class = "p" > ));< / span >
< a id = "__codelineno-6-7" name = "__codelineno-6-7" href = "#__codelineno-6-7" > < / a > < span class = "w" > < / span > < span class = "c1" > // 从第 k+1 个元素开始,保持堆的长度为 k< / span >
< a id = "__codelineno-6-8" name = "__codelineno-6-8" href = "#__codelineno-6-8" > < / a > < span class = "w" > < / span > < span class = "k" > for< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "kd" > let< / span > < span class = "w" > < / span > < span class = "nx" > i< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "nx" > k< / span > < span class = "p" > ;< / span > < span class = "w" > < / span > < span class = "nx" > i< / span > < span class = "w" > < / span > < span class = "o" > < < / span > < span class = "w" > < / span > < span class = "nx" > invertedNums< / span > < span class = "p" > .< / span > < span class = "nx" > length< / span > < span class = "p" > ;< / span > < span class = "w" > < / span > < span class = "nx" > i< / span > < span class = "o" > ++< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-6-9" name = "__codelineno-6-9" href = "#__codelineno-6-9" > < / a > < span class = "w" > < / span > < span class = "c1" > // 若当前元素小于堆顶元素,则将堆顶元素出堆、当前元素入堆< / span >
< a id = "__codelineno-6-10" name = "__codelineno-6-10" href = "#__codelineno-6-10" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "nx" > invertedNums< / span > < span class = "p" > [< / span > < span class = "nx" > i< / span > < span class = "p" > ]< / span > < span class = "w" > < / span > < span class = "o" > < < / span > < span class = "w" > < / span > < span class = "nx" > heap< / span > < span class = "p" > .< / span > < span class = "nx" > peek< / span > < span class = "p" > ())< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-6-11" name = "__codelineno-6-11" href = "#__codelineno-6-11" > < / a > < span class = "w" > < / span > < span class = "nx" > heap< / span > < span class = "p" > .< / span > < span class = "nx" > pop< / span > < span class = "p" > ();< / span >
< a id = "__codelineno-6-12" name = "__codelineno-6-12" href = "#__codelineno-6-12" > < / a > < span class = "w" > < / span > < span class = "nx" > heap< / span > < span class = "p" > .< / span > < span class = "nx" > push< / span > < span class = "p" > (< / span > < span class = "nx" > invertedNums< / span > < span class = "p" > [< / span > < span class = "nx" > i< / span > < span class = "p" > ]);< / span >
< a id = "__codelineno-6-13" name = "__codelineno-6-13" href = "#__codelineno-6-13" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-6-14" name = "__codelineno-6-14" href = "#__codelineno-6-14" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-6-15" name = "__codelineno-6-15" href = "#__codelineno-6-15" > < / a > < span class = "w" > < / span > < span class = "c1" > // 取出堆中元素< / span >
< a id = "__codelineno-6-16" name = "__codelineno-6-16" href = "#__codelineno-6-16" > < / a > < span class = "w" > < / span > < span class = "kd" > const< / span > < span class = "w" > < / span > < span class = "nx" > maxHeap< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "nx" > heap< / span > < span class = "p" > .< / span > < span class = "nx" > getMaxHeap< / span > < span class = "p" > ();< / span >
< a id = "__codelineno-6-17" name = "__codelineno-6-17" href = "#__codelineno-6-17" > < / a > < span class = "w" > < / span > < span class = "c1" > // 对堆中元素取相反数< / span >
< a id = "__codelineno-6-18" name = "__codelineno-6-18" href = "#__codelineno-6-18" > < / a > < span class = "w" > < / span > < span class = "kd" > const< / span > < span class = "w" > < / span > < span class = "nx" > invertedMaxHeap< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "nx" > maxHeap< / span > < span class = "p" > .< / span > < span class = "nx" > map< / span > < span class = "p" > ((< / span > < span class = "nx" > num< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > => < / span > < span class = "w" > < / span > < span class = "o" > -< / span > < span class = "nx" > num< / span > < span class = "p" > );< / span >
< a id = "__codelineno-6-19" name = "__codelineno-6-19" href = "#__codelineno-6-19" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "w" > < / span > < span class = "nx" > invertedMaxHeap< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-6-20" name = "__codelineno-6-20" href = "#__codelineno-6-20" > < / a > < span class = "p" > }< / span >
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >
< div class = "highlight" > < span class = "filename" > top_k.ts< / span > < pre > < span > < / span > < code > < a id = "__codelineno-7-1" name = "__codelineno-7-1" href = "#__codelineno-7-1" > < / a > < span class = "p" > [< / span > < span class = "kd" > class< / span > < span class = "p" > ]{}< / span > < span class = "o" > -< / span > < span class = "p" > [< / span > < span class = "nx" > func< / span > < span class = "p" > ]{< / span > < span class = "nx" > topKHeap< / span > < span class = "p" > }< / span >
< div class = "highlight" > < span class = "filename" > top_k.ts< / span > < pre > < span > < / span > < code > < a id = "__codelineno-7-1" name = "__codelineno-7-1" href = "#__codelineno-7-1" > < / a > < span class = "cm" > /* 基于堆查找数组中最大的 k 个元素 */< / span >
< a id = "__codelineno-7-2" name = "__codelineno-7-2" href = "#__codelineno-7-2" > < / a > < span class = "kd" > function< / span > < span class = "w" > < / span > < span class = "nx" > topKHeap< / span > < span class = "p" > (< / span > < span class = "nx" > nums< / span > < span class = "o" > :< / span > < span class = "w" > < / span > < span class = "kt" > number< / span > < span class = "p" > [],< / span > < span class = "w" > < / span > < span class = "nx" > k< / span > < span class = "o" > :< / span > < span class = "w" > < / span > < span class = "kt" > number< / span > < span class = "p" > )< / span > < span class = "o" > :< / span > < span class = "w" > < / span > < span class = "kt" > number< / span > < span class = "p" > []< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-7-3" name = "__codelineno-7-3" href = "#__codelineno-7-3" > < / a > < span class = "w" > < / span > < span class = "c1" > // 将堆中所有元素取反,从而用大顶堆来模拟小顶堆< / span >
< a id = "__codelineno-7-4" name = "__codelineno-7-4" href = "#__codelineno-7-4" > < / a > < span class = "w" > < / span > < span class = "kd" > const< / span > < span class = "w" > < / span > < span class = "nx" > invertedNums< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "nx" > nums< / span > < span class = "p" > .< / span > < span class = "nx" > map< / span > < span class = "p" > ((< / span > < span class = "nx" > num< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > => < / span > < span class = "w" > < / span > < span class = "o" > -< / span > < span class = "nx" > num< / span > < span class = "p" > );< / span >
< a id = "__codelineno-7-5" name = "__codelineno-7-5" href = "#__codelineno-7-5" > < / a > < span class = "w" > < / span > < span class = "c1" > // 将数组的前 k 个元素入堆< / span >
< a id = "__codelineno-7-6" name = "__codelineno-7-6" href = "#__codelineno-7-6" > < / a > < span class = "w" > < / span > < span class = "kd" > const< / span > < span class = "w" > < / span > < span class = "nx" > heap< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "ow" > new< / span > < span class = "w" > < / span > < span class = "nx" > MaxHeap< / span > < span class = "p" > (< / span > < span class = "nx" > invertedNums< / span > < span class = "p" > .< / span > < span class = "nx" > slice< / span > < span class = "p" > (< / span > < span class = "mf" > 0< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > k< / span > < span class = "p" > ));< / span >
< a id = "__codelineno-7-7" name = "__codelineno-7-7" href = "#__codelineno-7-7" > < / a > < span class = "w" > < / span > < span class = "c1" > // 从第 k+1 个元素开始,保持堆的长度为 k< / span >
< a id = "__codelineno-7-8" name = "__codelineno-7-8" href = "#__codelineno-7-8" > < / a > < span class = "w" > < / span > < span class = "k" > for< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "kd" > let< / span > < span class = "w" > < / span > < span class = "nx" > i< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "nx" > k< / span > < span class = "p" > ;< / span > < span class = "w" > < / span > < span class = "nx" > i< / span > < span class = "w" > < / span > < span class = "o" > < < / span > < span class = "w" > < / span > < span class = "nx" > invertedNums< / span > < span class = "p" > .< / span > < span class = "nx" > length< / span > < span class = "p" > ;< / span > < span class = "w" > < / span > < span class = "nx" > i< / span > < span class = "o" > ++< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-7-9" name = "__codelineno-7-9" href = "#__codelineno-7-9" > < / a > < span class = "w" > < / span > < span class = "c1" > // 若当前元素小于堆顶元素,则将堆顶元素出堆、当前元素入堆< / span >
< a id = "__codelineno-7-10" name = "__codelineno-7-10" href = "#__codelineno-7-10" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "nx" > invertedNums< / span > < span class = "p" > [< / span > < span class = "nx" > i< / span > < span class = "p" > ]< / span > < span class = "w" > < / span > < span class = "o" > < < / span > < span class = "w" > < / span > < span class = "nx" > heap< / span > < span class = "p" > .< / span > < span class = "nx" > peek< / span > < span class = "p" > ())< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-7-11" name = "__codelineno-7-11" href = "#__codelineno-7-11" > < / a > < span class = "w" > < / span > < span class = "nx" > heap< / span > < span class = "p" > .< / span > < span class = "nx" > pop< / span > < span class = "p" > ();< / span >
< a id = "__codelineno-7-12" name = "__codelineno-7-12" href = "#__codelineno-7-12" > < / a > < span class = "w" > < / span > < span class = "nx" > heap< / span > < span class = "p" > .< / span > < span class = "nx" > push< / span > < span class = "p" > (< / span > < span class = "nx" > invertedNums< / span > < span class = "p" > [< / span > < span class = "nx" > i< / span > < span class = "p" > ]);< / span >
< a id = "__codelineno-7-13" name = "__codelineno-7-13" href = "#__codelineno-7-13" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-7-14" name = "__codelineno-7-14" href = "#__codelineno-7-14" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-7-15" name = "__codelineno-7-15" href = "#__codelineno-7-15" > < / a > < span class = "w" > < / span > < span class = "c1" > // 取出堆中元素< / span >
< a id = "__codelineno-7-16" name = "__codelineno-7-16" href = "#__codelineno-7-16" > < / a > < span class = "w" > < / span > < span class = "kd" > const< / span > < span class = "w" > < / span > < span class = "nx" > maxHeap< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "nx" > heap< / span > < span class = "p" > .< / span > < span class = "nx" > getMaxHeap< / span > < span class = "p" > ();< / span >
< a id = "__codelineno-7-17" name = "__codelineno-7-17" href = "#__codelineno-7-17" > < / a > < span class = "w" > < / span > < span class = "c1" > // 对堆中元素取相反数< / span >
< a id = "__codelineno-7-18" name = "__codelineno-7-18" href = "#__codelineno-7-18" > < / a > < span class = "w" > < / span > < span class = "kd" > const< / span > < span class = "w" > < / span > < span class = "nx" > invertedMaxHeap< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "nx" > maxHeap< / span > < span class = "p" > .< / span > < span class = "nx" > map< / span > < span class = "p" > ((< / span > < span class = "nx" > num< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > => < / span > < span class = "w" > < / span > < span class = "o" > -< / span > < span class = "nx" > num< / span > < span class = "p" > );< / span >
< a id = "__codelineno-7-19" name = "__codelineno-7-19" href = "#__codelineno-7-19" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "w" > < / span > < span class = "nx" > invertedMaxHeap< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-7-20" name = "__codelineno-7-20" href = "#__codelineno-7-20" > < / a > < span class = "p" > }< / span >
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >